29

I'm doing a simple code

String splitString = "122$23$56$rt";
for(int i=0;i<splitString.split("$").length;i++){
   System.out.println("I GOT IS :: "+splitString.split("$")[i]);
}

When I split like

splitString.split("$")

It is giving me output [122$23$56$rt]

Why this is not splinting on '$'?

Maroun
  • 94,125
  • 30
  • 188
  • 241
user3243855
  • 353
  • 4
  • 9

8 Answers8

25

String.split() takes in regex as argument and $ is a metacharacter in Java regex API. Therefore, you need to escape it:

String splitString = "122$23$56$rt";
for(int i=0;i<splitString.split("\\$").length;i++){
   System.out.println("I GOT IS :: "+splitString.split("\\$")[i]);
}

Other metacharacters supported by the Java regex API are: <([{\^-=!|]})?*+.>

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

Is my favorite.

See Pattern#quote:

Returns a literal pattern String for the specified String.

Your code doesn't work because $ has a special meaning in regex, and since String#split takes a regex as an argument, the $ is not interpreted as the String "$", but as the special meta character $.

Maroun
  • 94,125
  • 30
  • 188
  • 241
8

Escape it. the split() method takes a regex: split("\\$")

mprivat
  • 21,582
  • 4
  • 54
  • 64
4

try something like this

String splitString = "122$23$56$rt";
for(int i=0;i<splitString.split("\\$").length;i++){
   System.out.println("I GOT IS :: "+splitString.split("$")[i]);
}

NOTE: split() uses a regular expression.

Your regular expression uses a special character ie $

$ is the regular expression for "end of line".

Maroun
  • 94,125
  • 30
  • 188
  • 241
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
3
String splitString = "122$23$56$rt";
for(int i=0;i<splitString.length;i++){
   System.out.println("Now you GOT this :: "+split(Pattern.quote("$")));
}

There are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".

So your $ is also metacharacter as defination says so you can't split using simple function. Though you must use pattern in this case.

Thanks..

Java Man
  • 1,854
  • 3
  • 21
  • 43
1

Escape it like

split("\\$")

instead of split("$")

Pandiyan Cool
  • 6,381
  • 8
  • 51
  • 87
0

String.split(), .match(), .replaceAll() are some of the methods that use RegEx pattern and so you should look at the javadoc of the Pattern class:

If your splitting character happen to be one of the pattern characters, you must escape it with \\, in this case your split call should be: .split("\\$")

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
TA Nguyen
  • 453
  • 1
  • 3
  • 8
0

It will not work because split() takes input as RegEx

String splitString = "122$23$56$rt";
for(int i=0;i<splitString.split("\\$").length;i++){
System.out.println("I GOT IS :: "+splitString.split("\\$")[i]);
}
work_in_progress
  • 747
  • 1
  • 10
  • 27