简体   繁体   English

String.split()-如何区分制表符分隔值和逗号分隔值?

[英]String.split() - How to diferentiate between tab separated values and comma separated values?

I'm collecting column data types from files using Java. 我正在使用Java从文件中收集列数据类型。 Some files are tab separated and some are comma-separated. 有些文件是制表符分隔的,而另一些则是逗号分隔的。 Came with the following code: 附带以下代码:

String[] valuesFromColumns = null;
                   if (line.split("\\s*\\t\\s*") != null) {

                    System.out.println("Separating by tab: " + line);
                            valuesFromColumns = line.split("\\s*\\t\\s*");

                    } else if(line.split("\\s*,\\s*")!=null) {
                    System.out.println("Separating by comma." + line);
                            valuesFromColumns = line.split("\\s*,\\s*");
                    } 

                    for(String s : valuesFromColumns) {
                        this.extractDataTypeFromTheLine(s);
                 }

The output is not what's expected: it mistakes comma separated files for tab-separated values. 输出不是预期的结果:它将逗号分隔的文件误认为制表符分隔的值。 What am I possibly doing wrong? 我可能做错了什么? Here is the output: 这是输出:

/Users/macbook/open_data_us/GPL94-tbl-1.txt
Separating by tab: 67023_at AI342132        Homo sapiens    Mar 11, 2009    Consensus sequence  GenBank Cluster Incl. AI342132:qt26c08.x1 Homo sapiens cDNA, 3' end /clone=IMAGE-1949102 /clone_end=3' /gb=AI342132 /gi=4079059 /ug=Hs.156499 /len=414  AI342132                            
String
String
String
...
/Users/macbook/open_data_us/Performance Metrics - Transportation.csv
Separating by tab: Alley Pot Hole,06/06/2011,Week,10,1.65,3,06/06/2011 - 06/12/2011
String
/Users/macbook/open_data_us/redditSubmissions.csv
Separating by tab: 0,1333178161,2012-03-31T14:16:01.093638-07:00,Expectation,35,rmun4,29,GifSound,6,1333203361,23,3,Gangsta_Raper
String

line.split does not return null when the regular expression doesn't find any matches. 当正则表达式找不到任何匹配项时,line.split不会返回null。 Instead it returns an array with the whole line as the first and only element. 而是返回一个以整行作为第一个也是唯一一个元素的数组。

Therefore the first condition ais always true. 因此,第一个条件总是成立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM