• Post Reply Bookmark Topic Watch Topic
  • New Topic

Query regarding split function in Java  RSS feed

 
Manish Vyas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.)
 
Rahul Gujarathi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!