• 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

Query regarding split function in Java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am using following java syntax for spliting a String named Code with character "|". To do that I need to prefix \\(two backslash) before that like this.. .
String [] SplitedVal=Code.split("\\|");


Can Anybody tell me, why is this required??

Thanx
Mans
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In regex pipe means "or" just like any regex in Unix based systems and all you know...

So if we want to use pipe as separator we have to put escape sequence for that, which is "\|" and for java program you have to put one more backslash so regex really gets "\|" ...

Regards,
Maulin
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this page for an overview of regular expressions...

http://java.sun.com/docs/books/tutorial/extra/regex/index.html

And this page for specific treatment of backslashes...

http://java.sun.com/docs/books/tutorial/extra/regex/pre_char_classes.html

(If I'm not mistaken, the second backslash is only required within String literals.)
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, thats right.

or operator (|) needs an escape character hence \|. But to get this escape character we need to prefix another escape character i.e. \\ for getting \.

Cheers
 
Manish Vyas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thax for your replies, But instead of pipe, if i use comma (,) character to split, I have to follow same way...
Why is it so?

Manish
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic