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 '$'?

0

8 Answers 8

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: <([{\^-=!|]})?*+.>

Sign up to request clarification or add additional context in comments.

Comments

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 $.

Comments

8

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

Comments

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".

Comments

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..

2 Comments

+1, but please provide an explanation with the answer.
Now that's an explanation! :)
1

Escape it like

split("\\$") 

instead of split("$")

Comments

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]); } 

Comments

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("\\$")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.