1
所以我正在編寫一個解析CSV的程序。我使用split方法將值分隔成一個字符串數組,但我讀過一些文章,它使用substring和indexOf更快。我基本上寫了我會用這兩種方法做什麼,似乎分裂會更好。有人可以解釋這是如何更好,或者如果我沒有正確使用這些方法?這是我寫的:拆分方法vs子串和索引
int indexOne = 0, indexTwo;
for (int i = 0; i < 4; i++) //there's four diff values in one line
{
if (line.indexOf(",", indexOne) != -1)
{
indexTwo = line.indexOf(",", indexOne);
lineArr[i] = line.substring(indexOne, indexTwo);
indexOne = indexTwo+1;
}
}
你可能會鏈接一些這些文章? –
考慮使用lodash或下劃線或類似的東西來處理這樣的事情。 – Michael
@AustinD這裏有一個鏈接http://demeranville.com/battle-of-the-tokenizers-delimited-text-parser-performance/有人把它放在stackexchange的評論這裏是該線程http://programmers.stackexchange.com/questions/221997 /最快路徑分割-a-delimited-string-in-java – trevalexandro