View Single Post
Old 12-29-2012, 10:47 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
In point of fact, using .split("[; ]") could very well lead to the wrong results if there *ARE* spaces in the string that is split.

Clearly if you had the string
Code:
1 potato; 2 potato; 3 potato more
You would get the array
Code:
1 potato;
 2 potato;
 3 potato more
if you simply split on ";" (notice the spaces at the beginning of the 2nd and 3rd elements.
).

But you would get
Code:
1
potato
[empty string]
2
potato
[empty string]
3
potato
more
if you split on "[; ]"

Possibly more useful would be to split on ";\\s*" ???

That would get you
Code:
1 potato;
2 potato;
3 potato more
getting rid of those leading spaces.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote