Why the split method does not support the $, * etc separator to split the string

import java.util.StringTokenizer;
class MySplit
{
  public static void main(String S[])
  {
    String settings = "12312$12121";
    StringTokenizer splitedArray = new StringTokenizer(settings,"$");

    String splitedArray1[] = settings.split("$");
        System.out.println(splitedArray1[0]);

    while(splitedArray.hasMoreElements())
        System.out.println(splitedArray.nextToken().toString());            
  }
}

In the above example, if I split the string with $, then it doesn't work fine, and if I split it with another character, then it works fine.

Why this is so, if it supports only the expression of a regular expression, why it works fine for :, ,, ;etc. characters.

+4
source share
5 answers

$ , String#split , $ "$", $. :

settings.split(Pattern.quote("$"))

Pattern#quote:

String .

... $, \\:

settings.split("\\$")

. , () .

splitedArray1[0], ArrayIndexOutOfBoundsException, $. :

if (splitedArray1.length == 0) {
    // return or do whatever you want 
    // except accessing the array
}
+6

Java, , split regex , , .

regex $ , :

settings.split("\\$");
+3

$ Java regex. , :

settings.split("\\$");

String.split docs:

.

, . .

:

, , .

+2

, split(String str) , str . , , , , .

, :

.split("\\$")

, :

String str = "This is 1st string.$This is the second string";
        for(String string : str.split("\\$"))
            System.out.println(string);

:

This is 1st string.
This is the second strin
+2

$- , , .

escape-\$, Java \$

Hope this helps. Greetings

+1
source

Source: https://habr.com/ru/post/1584858/


All Articles