当前位置: 技术问答>java相关
急! 如何将一个字符串,用指定的分隔符,转换成字符串数组,马上给分!!!
来源: 互联网 发布时间:2015-10-03
本文导语: jdk1.3中没有split 例如: String s="张三,李四,王五"; 要得到 String[] n n[0]="张三" n[1]="李四" n[2]="王五" 急! | public static String[] split(String source){ if(source==null||source.trim().equals("")) retu...
jdk1.3中没有split
例如:
String s="张三,李四,王五";
要得到
String[] n
n[0]="张三"
n[1]="李四"
n[2]="王五"
急!
例如:
String s="张三,李四,王五";
要得到
String[] n
n[0]="张三"
n[1]="李四"
n[2]="王五"
急!
|
public static String[] split(String source){
if(source==null||source.trim().equals(""))
return null;
StringTokenizer commaToker = new StringTokenizer(source,",");
String[] result = new String[commaToker.countTokens()];
int i=0;
while(commaToker.hasMoreTokens()){
result[i] = commaToker.nextToken();
i++;
}
return result;
}
if(source==null||source.trim().equals(""))
return null;
StringTokenizer commaToker = new StringTokenizer(source,",");
String[] result = new String[commaToker.countTokens()];
int i=0;
while(commaToker.hasMoreTokens()){
result[i] = commaToker.nextToken();
i++;
}
return result;
}
|
String[] s;
StringTokenizer st = new StringTokenizer('sadas,sadsad,ddasdwq,dwqd',",");
s=new String[st.countTokens()];
for(int i=0;st.hasMoreTokens();i++)
{s[i]=st.nextToken();
}
StringTokenizer st = new StringTokenizer('sadas,sadsad,ddasdwq,dwqd',",");
s=new String[st.countTokens()];
for(int i=0;st.hasMoreTokens();i++)
{s[i]=st.nextToken();
}
|
public int getCount(String str,String sign){//查找某一字符串中str,特定子串s的出现次数
if(str==null) return 0;
StringTokenizer s=new StringTokenizer(str,sign);
return s.countTokens();
}
public String[] getArray(String str,String sign){//按特定子串s为标记,将子串截成数组。
int count=getCount(str,sign);
int j=0;
String[] arr=new String[count];
for(int i=0;i
if(str==null) return 0;
StringTokenizer s=new StringTokenizer(str,sign);
return s.countTokens();
}
public String[] getArray(String str,String sign){//按特定子串s为标记,将子串截成数组。
int count=getCount(str,sign);
int j=0;
String[] arr=new String[count];
for(int i=0;i