![](/img/trans.png)
[英]oracle: how to convert string like '2000-01-01T01:01:01' to Date?
[英]how to split this string[LECT-3A,instr01,Instructor 01,teacher,instr1@learnet.com,,,,male,phone,,] as my requirement in java
你好,我从这样的 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
-使用split()
函数和,
作为分隔符来执行此操作。
例如:
String s = "Hello,this,is,vivek";
String[] arr = s.split(",");
您可以使用 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]male s [9]手机 [10] s[11]
你可以尝试这样的事情:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.