| Author |
Help required regarding string split function
|
Manojit Babu
Greenhorn
Joined: Oct 24, 2007
Posts: 3
|
|
Can any body make me understand what exactly \\s* means in split("\\s*,\\s*"); Thanks in advance
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
\s is white space in regural expressions. So it looks like the intention is to split the string based on occurences of a comma with zero or more white space characters round it.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Jan van Mansum
Ranch Hand
Joined: Oct 19, 2007
Posts: 74
|
|
See Documentation for split and for regular expressions In short the regex "\s*" means: "zero or more whitespace characters". In Java string literals you have to escape the backslash, so it becomes "\\s*" split will split your string into parts, using "zero or more whitespaces" as separators. E.g, after String[] parts = "This is a test".split("\\s*"); parts[0] contains "This", parts[1] contains "is", parts[2] contains "a", parts[3] contains "test". [ October 25, 2007: Message edited by: Jan van Mansum ]
|
SCJP 1.4, SCWCD 1.4
|
 |
Jan van Mansum
Ranch Hand
Joined: Oct 19, 2007
Posts: 74
|
|
OK, I just tried my example out (should have done that beforehand ) Anyway, the expression \s* doesn't work, that should be \s+ of course ("one or more spaces").
|
 |
bart zagers
Ranch Hand
Joined: Feb 05, 2003
Posts: 234
|
|
|
What about this topic???
|
 |
 |
|
|
subject: Help required regarding string split function
|
|
|