-1

大家好,我从这样的 csv 文件中得到了一个字符串

LECT-3A,instr01,Instructor 01,teacher,instr1@learnet.com,,,,male,phone,,

如何用逗号分割这个字符串我想要这样的数组

 s[0]=LECT-3A,s[1]=instr01,s[2]=Instructor 01,s[3]=teacher,s[4]=instr1@learnet.com,s[5]=,s[6]=,s[7]=,s[8]=male,s[9]=phone,s[10]=,s[11]=

谁能帮我把上面的字符串拆分为我的数组

thank u inadvance
4

3 回答 3

1

-使用split()带有分隔符,的功能来执行此操作。

例如:

String s = "Hello,this,is,vivek";

String[] arr = s.split(",");
于 2012-11-23T10:47:35.090 回答
0

您可以使用 limit 参数来执行此操作:

limit 参数控制应用模式的次数,因此会影响结果数组的长度。如果限制 n 大于零,则模式将最多应用 n - 1 次,数组的长度将不大于 n,并且数组的最后一个条目将包含最后一个匹配分隔符之外的所有输入。如果 n 为非正数,则该模式将尽可能多地应用,并且数组可以具有任意长度。如果 n 为零,则该模式将被应用尽可能多的次数,数组可以有任意长度,并且尾随的空字符串将被丢弃。

例子:

String[]
ls_test = "LECT-3A,instr01,Instructor 01,teacher,instr1@learnet.com,,,,male,phone,,".split(",",12);

int cont = 0;

for (String ls_pieces : ls_test)
    System.out.println("s["+(cont++)+"]"+ls_pieces);

输出:

s[0]LECT-3A s[1]instr01 s[2]讲师01 s[3]老师s[4]instr1@learnet.com s[5]s[6]s[7]s[8]男s [9]电话[10][11]

于 2012-11-23T11:01:00.853 回答
0

你可以尝试这样的事情:

String str = "LECT-3A,instr01,Instructor 01,teacher,instr1@learnet.com,,,,male,phone,,";
List<String> words = new ArrayList<String>();
int current = 0;
int previous = 0;
while((current = str.indexOf(",", previous)) != -1)
{           
    words.add(str.substring(previous, current));
    previous = current + 1;
}

String[] w = words.toArray(new String[words.size()]);
for(String section : w)
{
    System.out.println(section);
}

这产生:

LECT-3A

instr01

Instructor 01

teacher

instr1@learnet.com







male

phone
于 2012-11-23T11:06:20.267 回答