• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help required regarding string split function  RSS feed

 
Manojit Babu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body make me understand what exactly \\s* means in

split("\\s*,\\s*");

Thanks in advance
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\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.
 
Jan van Mansum
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jan van Mansum
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about this topic???
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!