Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to perform split operations for special characters ~!^* etc..?

 
antsi klando
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when the String.split("@!~*"); is used it does not split properly.Can I know how to escape from this?
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats because split() method takes regex not simple string. If you wan't to use some of the regex's special symbols, you must escape them with double backslash.
 
antsi klando
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, I find that some special characters escape by prefixing it with a double backslash "\\",some with "|".How will we know when to use what?
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

To continue: you can find out lots about regular expressions here.
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
antsi klando wrote:But, I find that some special characters escape by prefixing it with a double backslash "\\",some with "|".How will we know when to use what?

That is not correct. You can not escape anything with |. By writing | you'r regex results in expression "something or something else". So, if you write in java:


, the result will be


Let's say, we can think of such "A|B" expression like:
"All the places in string that is ether capital A or capital B"
Each fragment in string matching such expression will be splitted out.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No character is escaped with \\ nor with |.

The | is an or operator, and you can find out about in from the link I gave you a few minutes ago. It is not used for escaping.
Any meta-characters to be used as their literal value must be escaped. For example . means anything (except line end, usually), so to get it to mean full stop you have to escape it with \ So you use the escape sequence \.
If you write a String literal you have to escape the \ with another \. So you actually have two escape sequences, \\ to give you \ and then \. to give . If you try this class, you can experiment with different escape sequences, eg . \. \s at the option pane.
campbell@???:~/java> java EscapeDemo "Go. Go now. Dont wait. Dont look back"
Go.
Go
now.
Dont
wait.
Dont
look
back
campbell@???:~/java> java EscapeDemo "Go. Go now. Don't wait. Don't look back"
campbell@???:~/java> java EscapeDemo "Go. Go now. Don't wait. Don't look back"
Go
Go now
Don't wait
Don't look back
campbell@linux-mc9j:
In one case I entered \s, in another . and in the third \. That is because the OptionPane dialogue sends back the String entered, without requiring an additional \ to escape it. I'll leave you to work out why you get no output from one of them.
Oddly enough, if you enter those escape sequences at the command line you are liable to get different results. Presumably because the escape sequences are changed by the operating system before they reach the application.

If you write a String literal, you have to escape the \ to \\. Sometimes you have to escape the escape, so you get \\\\ or even \\\\\\\\ !
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
antsi klando wrote:when the String.split("@!~*"); is used it does not split properly.Can I know how to escape from this?

If you want to split on this exact string, you can use Pattern.quote:
 
antsi klando
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic