How can I split out individual column values from each line in a text file?

谁都会走 提交于 2019-12-23 03:50:57

问题


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


回答1:


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).




回答2:


sed 's/  */\n/g' < input

Two spaces there btw.




回答3:


Check the StringTokenizer class.



来源:https://stackoverflow.com/questions/1236113/how-can-i-split-out-individual-column-values-from-each-line-in-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!