File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String.split( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String.split("").......Confusion." Watch "String.split("").......Confusion." New topic
Author

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

Shailesh Vohra
Greenhorn

Joined: Feb 15, 2010
Posts: 9
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

Joined: Feb 02, 2010
Posts: 247
You got very close to your answer. Change the split() method call so that you use "\\$"
as the argument:


SCJP 6
Shailesh Vohra
Greenhorn

Joined: Feb 15, 2010
Posts: 9
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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
OK, Shailesh, I get it. Change the split() method call to as shown below:


Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

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


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Shailesh Vohra
Greenhorn

Joined: Feb 15, 2010
Posts: 9
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

Joined: Feb 15, 2010
Posts: 9
thanks nidhi.
i will surely go through the Regex in more details.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Shailesh Vohra wrote:Also do "|" also have special meaning in regex??



yes
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
Look at http://www.regular-expressions.info/quickstart.html

a|b means to look for a or b
Dhiraj Bankar
Greenhorn

Joined: Jan 10, 2010
Posts: 2
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]);
}

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.split("").......Confusion.
 
Similar Threads
needs explanation for the output
HashSet question
how many eligible for Garbage Collection
String and String Object Problem
Strings and their constant pool