当前位置: 技术问答>java相关
StringTokenizer的问题!!!!
来源: 互联网 发布时间:2015-10-22
本文导语: 我用StringTokenizer分割 String str = "abcd'efg'cd''df"; '为分隔符 我应该要的是 "abcd" , "efg" , "cd", "" , "df" 5 个. 它却只给我 "abcd" , "efg" , "cd", "df" 4 个. 问大家有没有举刀这样的问题,有没有办法解决? | ...
我用StringTokenizer分割
String str = "abcd'efg'cd''df";
'为分隔符
我应该要的是
"abcd" , "efg" , "cd", "" , "df" 5 个.
它却只给我
"abcd" , "efg" , "cd", "df" 4 个.
问大家有没有举刀这样的问题,有没有办法解决?
String str = "abcd'efg'cd''df";
'为分隔符
我应该要的是
"abcd" , "efg" , "cd", "" , "df" 5 个.
它却只给我
"abcd" , "efg" , "cd", "df" 4 个.
问大家有没有举刀这样的问题,有没有办法解决?
|
别用StringTokenizer,用最土的办法:
String str = "abcd'efg'cd''df";
while ( str.indexOf("'")!=-1 ) {
System.out.println(str.substring(0,str.indexOf("'")));
str = str.substring(str.indexOf("'")+1);
}
System.out.println(str);
String str = "abcd'efg'cd''df";
while ( str.indexOf("'")!=-1 ) {
System.out.println(str.substring(0,str.indexOf("'")));
str = str.substring(str.indexOf("'")+1);
}
System.out.println(str);
|
用StringTokenizer(str, "'", true)来构造
import java.util.*;
import java.text.*;
public class Test {
public static void main(String args[]) throws Exception{
String strLine = "abcd'efg'cd''df";
StringTokenizer strTokenizer = new StringTokenizer(strLine,"'",true);
String strTmp="";
int i=0;
while ( strTokenizer.hasMoreElements())
{
String s = strTokenizer.nextToken();
if( s.equals(",")) {
System.err.println("strTmp" + (i++) + "=" + strTmp);
strTmp="";
}else
strTmp = s;
}
System.err.println("strTmp" + (i++) + "=" + strTmp);
}
}
import java.util.*;
import java.text.*;
public class Test {
public static void main(String args[]) throws Exception{
String strLine = "abcd'efg'cd''df";
StringTokenizer strTokenizer = new StringTokenizer(strLine,"'",true);
String strTmp="";
int i=0;
while ( strTokenizer.hasMoreElements())
{
String s = strTokenizer.nextToken();
if( s.equals(",")) {
System.err.println("strTmp" + (i++) + "=" + strTmp);
strTmp="";
}else
strTmp = s;
}
System.err.println("strTmp" + (i++) + "=" + strTmp);
}
}