| Author |
how to perform split operations for special characters ~!^* etc..?
|
antsi klando
Greenhorn
Joined: May 10, 2011
Posts: 24
|
|
|
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
Greenhorn
Joined: May 10, 2011
Posts: 24
|
|
|
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: 32708
|
|
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: 32708
|
|
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: 19216
|
|
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
How To Ask Questions How To Answer Questions
|
 |
antsi klando
Greenhorn
Joined: May 10, 2011
Posts: 24
|
|
thank you so much
|
 |
 |
|
|
subject: how to perform split operations for special characters ~!^* etc..?
|
|
|