Java StringTokenizer属于 java.util 包,用于分隔字符串。
StringTokenizer 构造方法:
StringTokenizer 常用方法:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String str = "runoob,google,taobao,facebook,zhihu";
// 以 , 号为分隔符来分隔字符串
StringTokenizer st=new StringTokenizer(str,",");
while(st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
输出结果为:
runoob
google
taobao
facebook
zhihu
import java.util.*;
public class Main
{
public static void main(String args[])
{
System.out.println("使用第一种构造函数:");
StringTokenizer st1 = new StringTokenizer("Hello Runoob How are you", " ");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
System.out.println("使用第二种构造函数:");
StringTokenizer st2 = new StringTokenizer("JAVA : Code : String", " :");
while (st2.hasMoreTokens())
System.out.println(st2.nextToken());
System.out.println("使用第三种构造函数:");
StringTokenizer st3 = new StringTokenizer("JAVA : Code : String", " :", true);
while (st3.hasMoreTokens())
System.out.println(st3.nextToken());
}
}
输出结果为:
使用第一种构造函数:
Hello
Runoob
How
are
you
使用第二种构造函数:
JAVA
Code
String
使用第三种构造函数:
JAVA
:
Code
:
String