请看sun的文档说明 public StringTokenizer(String str, String delim)Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens. Parameters: str - a string to be parsed. delim - the delimiters.这里的意思是说把delim分隔符里的每个字符都当作分隔符来对待,所以会出现上述的结果!! 解决的办法用split; 请看老贴http://community.csdn.net/Expert/topic/2965/2965683.xml?temp=.2673761
并且不推荐使用咯........ StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
這是我的測試程序,完全正確阿 import java.util.regex.*;public class RG { public static void main(String[] args) {
String s = "123 %p%alkjsdfj%palskdjf p lajsdf"; String[] r = s.split("%p"); System.out.println("/**********************************/"); System.out.println(s); for(int i = 0 ; i < r.length; i++) { System.out.println(r[i]); } } }
public StringTokenizer(String str, String delim)Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens. Parameters:
str - a string to be parsed.
delim - the delimiters.这里的意思是说把delim分隔符里的每个字符都当作分隔符来对待,所以会出现上述的结果!!
解决的办法用split;
请看老贴http://community.csdn.net/Expert/topic/2965/2965683.xml?temp=.2673761
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
import java.util.regex.*;public class RG
{
public static void main(String[] args)
{
String s = "123 %p%alkjsdfj%palskdjf p lajsdf";
String[] r = s.split("%p");
System.out.println("/**********************************/");
System.out.println(s);
for(int i = 0 ; i < r.length; i++)
{
System.out.println(r[i]);
}
}
}
StringTokenizer 在 1.4 以后已经被 deprecate 了
现在解决 String 类问题应该用 Regular Expressions
public final static String SPLIT = "%p ";private String[] getElements( String str, int len )
{
String[] elements = new String[len];
int begPos = 0;
int endPos = -1;
int nElem = 0;
do
{
endPos=str.indexOf( SPLIT, endPos+1 );
if(endPos>-1)
{
elements[nElem] = str.substring( begPos,endPos );
begPos = endPos + 1;
nElem ++;
}
}while ( endPos > -1 );
return elements;
}
private int countElements(String str)
{
int count = 0;
int pos = -1;
do
{
pos=str.indexOf(SPLIT, pos+1);
if( pos > -1 )
{
count ++;
}
}
while ( pos >- 1 );
return count;
}