• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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("\\|").


 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, Shailesh, I get it. Change the split() method call to as shown below:


 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks nidhi.
i will surely go through the Regex in more details.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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




yes
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at http://www.regular-expressions.info/quickstart.html

a|b means to look for a or b
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);
}

 
That new kid is a freak. Show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic