Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a String into tokens

I would like to split a String into tokens that are stored in an array. However i don not think I am able to use delimiters as the strings information is not separated by a specific set of characters. The information is however always separated by varying amounts of white space.

Like this:

0      147    530.936        1    656.336   -1.12709    656.336   -0.52921 -0.0131993  -0.882138        0       20        0        0   0.878423          0 1.4013E-045          0    

My question is, is there a way to use the varying amounts of white space as delimiters, in order to tokenize the string?

like image 507
user2682570 Avatar asked Jul 21 '26 09:07

user2682570


1 Answers

How about

String[] tokens = yourString.split("\\s+");

Split uses regex and in regex

  • to represent any whitespace you can use "\\s"
  • to say "one or more times" use +
like image 105
Pshemo Avatar answered Jul 23 '26 23:07

Pshemo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!