解析字符串以查找下一个定界符-处理 [英] Parsing a string to find next delimiter - Processing
问题描述
所以这里的想法是,我将一个.csv放入一个字符串中,并且每个值都需要存储到一个变量中.我不确定如何正确解析字符串来做到这一点.
So the idea here is that I'm taking a .csv into a string and each value needs to be stored into a variable. I am unsure how to properly parse a string to do this.
我的想法是一个看起来像这样的函数
My idea is a function that looks like
final char delim = ',';
int nextItem(String data, int startFrom) {
if (data.charAt(startFrom) != delim) {
return data.charAt(startFrom)
} else {
return nextItem(data, startFrom + 1);
}
}
所以如果我通过了类似的事情
so if I passed it something like
nextItem("45,621,9", 0);
它将返回45
如果我通过了
nextItem("45,621,9", 3);
它将返回621
我不确定是否可以正确地进行递归设置,但是我想我也可以使用For循环,只有真正的规定是我不能使用Substring方法.
I'm not sure if I have that setup properly to be recursive, but I could also use a For loop I suppose, only real stipulation is I can't use the Substring method.
推荐答案
处理过程附带一个split()
函数,该函数可以完全满足您的描述.
Processing comes with a split()
function that does exactly what you're describing.
来自参考:
String men = "Chernenko,Andropov,Brezhnev";
String[] list = split(men, ',');
// list[0] is now "Chernenko", list[1] is "Andropov"...
在后台使用String#split()
函数,例如H. Sodi的答案,但是您应该只使用此函数,而不要定义自己的函数.
Behind the scenes it's using the String#split()
function like H. Sodi's answer, but you should just use this function instead of defining your own.
这篇关于解析字符串以查找下一个定界符-处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!