[英]how to split this string[LECT-3A,instr01,Instructor 01,teacher,instr1@learnet.com,,,,male,phone,,] as my requirement in java
hello every one i got a string from csv file like this你好,我从这样的 csv 文件中得到了一个字符串
LECT-3A,instr01,Instructor 01,teacher,instr1@learnet.com,,,,male,phone,,
how to split this string with comma i want the array like this如何用逗号分割这个字符串我想要这样的数组
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]=
can anyone please help me how to split the above string as my array任何人都可以帮助我如何将上面的字符串拆分为我的数组
thank u inadvance
- Use the split()
function with ,
as delimeter to do this. -使用
split()
函数和,
作为分隔符来执行此操作。
Eg:例如:
String s = "Hello,this,is,vivek";
String[] arr = s.split(",");
you can use the limit parameter to do this:您可以使用 limit 参数来执行此操作:
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. limit 参数控制应用模式的次数,因此会影响结果数组的长度。 If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.
如果限制 n 大于零,则该模式将最多应用 n - 1 次,数组的长度将不大于 n,并且数组的最后一个条目将包含最后一个匹配的分隔符之外的所有输入。 If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.
如果 n 为非正数,则该模式将被应用尽可能多的次数,并且数组可以具有任意长度。 If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
如果 n 为零,则该模式将被应用尽可能多的次数,数组可以具有任意长度,并且将丢弃尾随的空字符串。
Example:例子:
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);
output:输出:
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] 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]male s [9]手机 [10] s[11]
You could try something like so:你可以尝试这样的事情:
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);
}
This yields:这产生:
LECT-3A
instr01
Instructor 01
teacher
instr1@learnet.com
male
phone
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.