• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.split("").......Confusion.

 
Shailesh Vohra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running below code :

String s3 = "ABC$$DEF$$P QR$$XYZ";
String []s2 = s3.split("$$");

for (int i = 0; i < s2.length; i++) {
System.out.println(s2[i]);
}

Here i am using regex : "$$" as a delimiter.
but output i am getting is same as inp String. i.e ABC$$DEF$$P QR$$XYZ

But this is not my desired output.
i want output as
ABC
DEF
P QR
XYZ.

Please help to explain me. also when i use delimiter "|" why do i have to use escape sequence.
String.split("\\|").


 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got very close to your answer. Change the split() method call so that you use "\\$"
as the argument:
 
Shailesh Vohra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Larry.
But i want to use "$$" as a delimiter and not "$".
and i am passing it correctly (hope so). as String.split("$$")
Please explain why its not printing desired output.
 
Henry Wong
author
Marshal
Pie
Posts: 21221
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shailesh Vohra wrote:Please explain why its not printing desired output.


The split() method takes a regular expression -- and "$" has special meaning in a regex. If you want an actual "$", you will need to escape it. And if you want two "$", then you need to escape both of them.

Henry
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, Shailesh, I get it. Change the split() method call to as shown below:


 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shailesh,

Changing your program to this will get the output you are looking for:


The reason that you have to escape $ and | is that both these characters have a special meaning in Regex.
$ indicates 'end of a line' for boundry matches
| indicates the 'or' logical operator

Check out the api document of Pattern class for all the details you need about different characters.

HTH,
Nidhi
 
Shailesh Vohra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Larry and Henry for that explanation.
Also do "|" also have special meaning in regex??
because i need to use String.split("\\|").
 
Shailesh Vohra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks nidhi.
i will surely go through the Regex in more details.
 
Henry Wong
author
Marshal
Pie
Posts: 21221
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shailesh Vohra wrote:Also do "|" also have special meaning in regex??



yes
 
Kuba Zygmunt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at http://www.regular-expressions.info/quickstart.html

a|b means to look for a or b
 
Dhiraj Bankar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shailesh Vohra wrote:I am running below code :

String s3 = "ABC$$DEF$$P QR$$XYZ";
String []s2 = s3.split("$$");

for (int i = 0; i < s2.length; i++) {
System.out.println(s2[i]);
}

Here i am using regex : "$$" as a delimiter.
but output i am getting is same as inp String. i.e ABC$$DEF$$P QR$$XYZ

But this is not my desired output.
i want output as
ABC
DEF
P QR
XYZ.

Please help to explain me. also when i use delimiter "|" why do i have to use escape sequence.
String.split("\\|").

you shold try this.


String s3 = "ABC$$DEF$$P QR$$XYZ";
String []s2 = s3.split("\\$\\$");

for (int i = 0; i < s2.length; i++) {
System.out.println(s2[i]);
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic