jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes how to perform split operations for special characters ~!^* etc..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to perform split operations for special characters ~!^* etc..?" Watch "how to perform split operations for special characters ~!^* etc..?" New topic
Author

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

antsi klando
Ranch Hand

Joined: May 10, 2011
Posts: 46
when the String.split("@!~*"); is used it does not split properly.Can I know how to escape from this?
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

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

Joined: May 10, 2011
Posts: 46
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

Joined: Oct 13, 2005
Posts: 38513
    
  23
And welcome to the Ranch

To continue: you can find out lots about regular expressions here.
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

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

Joined: Oct 13, 2005
Posts: 38513
    
  23
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

Joined: Oct 27, 2005
Posts: 19684
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
antsi klando
Ranch Hand

Joined: May 10, 2011
Posts: 46
thank you so much
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to perform split operations for special characters ~!^* etc..?