1,105,633 Community Members

[Basic] splitting a string

Member Avatar
FALL3N
Junior Poster in Training
88 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

ok, I'm pretty embarrassed to be posting such a simple question, I feel like I've done this in much harder applications like a million times...

String[] wordVar = text.split("$");

Why does the above code not split the String 'text' at each occurrence of a "$"? Is the "$" a special character or something? when I execute this code and then get text.length; it returns 1 regardless of the number of "$"s..

What am I doing wrong?

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Yes, the param for split is Regex, and $ is a special char in regular expressions. You have to escape it with a backslash to use it as a literal $

Member Avatar
FALL3N
Junior Poster in Training
88 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

But this does not work either...

String[] text = word.split("\$");

should I declare the "\$" as part of a Pattern? Or a Matcher? or use ' instead of "? I know this started out ridiculous, and just keeps getting more ridiculous, but I am really getting thrown by this.

Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

But this does not work either...

String[] text = word.split("\$");

should I declare the "\$" as part of a Pattern? Or a Matcher? or use ' instead of "? I know this started out ridiculous, and just keeps getting more ridiculous, but I am really getting thrown by this.

[EDIT] See below post

Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
1
 

But this does not work either...

String[] text = word.split("\$");

should I declare the "\$" as part of a Pattern? Or a Matcher? or use ' instead of "? I know this started out ridiculous, and just keeps getting more ridiculous, but I am really getting thrown by this.

your biggest problem might be you are using one back slash:

String h="re$rt$yy";
        String[] a=h.split("\\$");
        for(int i=0;i<a.length;i++) {
        System.out.println(a[i]);
    }
Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
2
 

That's right. It's messy, but $ is a special char in a RegEx and needs to be escaped with a \. But \ is a special char in Java string literal, and needs to be escaped with a second \. So to get a literal $ in a RegEx you need the Java string literal "\\$"

Member Avatar
FALL3N
Junior Poster in Training
88 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

hmm.. That's why it was not working right... thanks guys!

Question Answered as of 2 Years Ago by JamesCherrill and DavidKroukamp
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: