当前位置: 技术问答>java相关
分离下面的字符串的方法?
来源: 互联网 发布时间:2015-08-31
本文导语: String Items="111111,222222,333333,444444"; String[] Item =new String[?];//?=多少 如何实现下面的情况? Item[0]="111111"; Item[2]="222222"; ...... Item[3]="444444"; | 使用StringTokenizer,要小心对 String Items="111111,222222,33...
String Items="111111,222222,333333,444444";
String[] Item =new String[?];//?=多少
如何实现下面的情况?
Item[0]="111111";
Item[2]="222222";
......
Item[3]="444444";
String[] Item =new String[?];//?=多少
如何实现下面的情况?
Item[0]="111111";
Item[2]="222222";
......
Item[3]="444444";
|
使用StringTokenizer,要小心对
String Items="111111,222222,333333,,,444444";
的解析.
String Items="111111,222222,333333,,,444444";
的解析.
|
public static String[] split(String str, char delim)
{
if(str == null || str.length() == 0)
return new String[0];
Vector v = new Vector();
int i;
while((i = str.indexOf(delim)) >= 0)
{
v.addElement(str.substring(0, i));
str = str.substring(i + 1);
}
v.addElement(str);
String strs[] = new String[v.size()];
v.copyInto(strs);
return strs;
}
public static String[] split(String str, String delim)
{
if(str == null || str.length() == 0)
return new String[0];
Vector v = new Vector();
for(StringTokenizer tok = new StringTokenizer("X" + str, delim); tok.hasMoreTokens(); v.addElement(tok.nextToken()));
String strs[] = new String[v.size()];
v.copyInto(strs);
strs[0] = strs[0].substring(1);
return strs;
}
{
if(str == null || str.length() == 0)
return new String[0];
Vector v = new Vector();
int i;
while((i = str.indexOf(delim)) >= 0)
{
v.addElement(str.substring(0, i));
str = str.substring(i + 1);
}
v.addElement(str);
String strs[] = new String[v.size()];
v.copyInto(strs);
return strs;
}
public static String[] split(String str, String delim)
{
if(str == null || str.length() == 0)
return new String[0];
Vector v = new Vector();
for(StringTokenizer tok = new StringTokenizer("X" + str, delim); tok.hasMoreTokens(); v.addElement(tok.nextToken()));
String strs[] = new String[v.size()];
v.copyInto(strs);
strs[0] = strs[0].substring(1);
return strs;
}
|
String Items="111,12,3123,1244,7647";
int i=1;
while (Items.indexOf(',',i)!=-1){
i++;
}
String[] Item=new String[i+1];
i=0;
while (Items.indexOf(',')!=-1){
Item[i]=Items.substring(0,Items.indexOf(','));
Items=Items.substring(Items.indexOf(',')+1,Items.length());
i++;
}
Item[i]=Items;
int i=1;
while (Items.indexOf(',',i)!=-1){
i++;
}
String[] Item=new String[i+1];
i=0;
while (Items.indexOf(',')!=-1){
Item[i]=Items.substring(0,Items.indexOf(','));
Items=Items.substring(Items.indexOf(',')+1,Items.length());
i++;
}
Item[i]=Items;
|
java.util.StringTokenizer
|
这样就可以了。
String Items="111111,222222,333333,444444";
StringTokenizer strToken=new StringTokenizer(Items,",");
String[] Item = new String[strToken.countTokens()];
String Items="111111,222222,333333,444444";
StringTokenizer strToken=new StringTokenizer(Items,",");
String[] Item = new String[strToken.countTokens()];
|
int k=0;
while(strToken.hasMoreTokens())
{
strDate[k]=strToken.nextToken();
k++;
}
while(strToken.hasMoreTokens())
{
strDate[k]=strToken.nextToken();
k++;
}
|
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