45

I want to split a string which has content like this:

a$b$c

but when I use:

String data=...
data.split("$");

it does not recognize $ and do not split string but when I replace $ by some Letter like X it works. does anyone has any Idea?

6 Answers 6

107

The split function takes a regular expression, not a string, to match. Your regular expression uses a special character - in this case '$' - so you would need to change it to escape that character:

String line = ...
String[] lineData = line.split("\\$");

Also note that split returns an array of strings - Strings are immutable, so they cannot be modified. Any modifications made to the String will be returned in a new String, and the original will not be changed. Hence the lineData = line.split("\\$"); above.

6
  • 4
    Technically, the regex argument to String.split() is an object of type String. But the method expects it to be in a regular expression format.
    – David
    Jan 30, 2010 at 18:09
  • Yes, that is correct - apologies if I did not make that clear, or if I was confusing in that statement.
    – aperkins
    Jan 30, 2010 at 18:10
  • 10
    Split returns String[], not String.
    – ColinD
    Jan 30, 2010 at 18:37
  • 1
    @ColinD - you are correct. Self dope slap procedure has been initiated
    – aperkins
    Jan 30, 2010 at 19:26
  • 1
    Because String.split(String param) the "param" needs to be a regular expression.
    – RRTW
    Jun 26, 2013 at 8:34
15

The split method accept a String as the first parameter that is then interpreted as a Regular Expression.

The dollar sign is a specific operator in regular expressions and so you have to escape it this way to get what you want:

String data = ...
String[] parts = data.split("\\$");

Or, if the delimiter may change you can be more general this way:

String data = ...
String[] parts = data.split(java.util.regex.Pattern.quote("$"));
3

split() uses a regular expression as parameter. You have to call split( "\\$" ), because $ is the regular expression for "end of line".

3

$ is a special character in regular expressions representing the end of the line. To match a dollar sign, use "\\$".

3

String.split() in Java takes a String argument which is a regular expression. The '$' character in a regex means the end of a line. You can use an escape sequence ("\\$") if you are looking for a dollar sign in the string.

Sources:

String - Java API

Pattern - Java API

2
  • Don't forget to escape the \ aswell.. so make that ("\\$")
    – Tim
    Jan 30, 2010 at 18:25
  • @Tim - Yes, that's what I wrote... it just only showed up as a single backslash. I'm still pretty new to SO and its editor.
    – David
    Jan 30, 2010 at 19:01
-1

You may have an issue with the uniCode characters for non-breaking spaces. Try...

String[] elements = myString.split("[\\s\\xA0]+"); //include uniCode non-breaking

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.