String str = "The indictment said the defendants "
+ "had collected geographical data indicating "
+ "thousands of people would be killed in the "
+ "chemical blast";
怎么用StringTokenizer
解析到StrAry数组里
谢谢
+ "had collected geographical data indicating "
+ "thousands of people would be killed in the "
+ "chemical blast";
怎么用StringTokenizer
解析到StrAry数组里
谢谢
public class Test
{
public static void main(String[] args)
{
String str = "The indictment said the defendants "
+ "had collected geographical data indicating "
+ "thousands of people would be killed in the "
+ "chemical blast";
String[] strAry = str.split("\\s");
// 打印出来
for (int i = 0; i < strAry.length; i ++)
System.out.println(strAry[i]);
}}
F:\>javac Test.javaF:\>java Test
The
indictment
said
the
defendants
had
collected
geographical
data
indicating
thousands
of
people
would
be
killed
in
the
chemical
blastF:\>
String str;//你给定
String arr = pattern.split(str);
String token 字符串节点 分解。
java.util.StringTokenizer str = java.util.StringTokenizer(str," ");//空格分隔
int count=0;
StringTokenizer str1=new StringTokenizer(str," ");
arg=new String[str1.countTokens()];
while(str1.hasMoreTokens())
{
arg[count]=str1.nextToken();
count++;
}
----------------------我也不懂,我理解来字符表示器
+ "had collected geographical data indicating "
+ "thousands of people would be killed in the "
+ "chemical blast";
StringTokenizer st = new StringTokenizer(str," ");
String[] arrays = new String[st.countTokens];
for(int i = 0; i<arrays.length; i++){
if(st.hasMoreTokens)
arrays[i] = st.nextToken();
}
public class Test
{
public static void main(String args[])
{
String str= "The indictment said the defendants "
+ "had collected geographical data indicating "
+ "thousands of people would be killed in the "
+ "chemical blast";
StringTokenizer str1=new StringTokenizer(str," "); //字符解析器创建对象
String[] StrAry=new String[str1.countTokens()]; //创建一个数组
//输出
while(str1.hasMoreTokens())
{
int i=0;
StrAry[i]=str1.nextToken();
System.out.println(StrAry[i]);
}
}
}
其中 StringTokenizer 是一个字符串分析器
nextToken()方法是逐个获取下一个语言符号
hasMoreToken()方法是只要字符串还有语言符号就返回ture
countTokens()方法是得到字符串一共有多少个语言符号