views:

83

answers:

3

I have lines in an ASCII text file that I need to parse. The columns are separated by a variable number of spaces, for instance:

column1 column2     column3

How would i split this line to return an array of only the values?

thanks

+7  A: 
String testvar = "Some   Data    separated  by     whitespace";
String[] vals = testvar.split("\\s+");

\s means a whitespace character, the + means 1 or more. .split() splits a string into parts divided by the specified delimiter (in this case 1 or more whitespace characters).

Amber
it doesn't compile, but .split("\\s+") does, i'll see if that works
bmw0128
the "\\s+" works, thanks a lot!!
bmw0128
Oh, yeah, sorry - my bad on that one. ;) I'll tweak the answer for others.
Amber
A: 
sed 's/  */\n/g' < input

Two spaces there btw.

Joseph Silvashy
A: 

Check the StringTokenizer class.

Thorbjørn Ravn Andersen