StringTokenizer类的使用方法
StringTokenizer:字符串分隔解析类型
属于:java.util包。
1、构造函数。
1. StringTokenizer(String str) :构造一个用来解析str的StringTokenizer对象。java默认的分隔符是“空格”、“制表符(‘\t’)”、“换行符(‘\n’)”、“回车符(‘\r’)”。
2. StringTokenizer(String str, String delim) :构造一个用来解析str的StringTokenizer对象,并提供一个指定的分隔符。
3. StringTokenizer(String str, String delim, boolean returnDelims) :构造一个用来解析str的StringTokenizer对象,并提供一个指定的分隔符,同时,指定是否返回分隔符。
2、方法。
说明:
1. 所有方法均为public;
2. 书写格式:[修饰符] <返回类型> <方法名([参数列表])>
如:
static int parseInt(String s) 表示:此方法(parseInt)为类方法(static),返回类型为(int),方法所需参数为String类型。
1. int countTokens() :返回nextToken方法被调用的次数。如果采用构造函数1和2,返回的就是分隔符数量(例2)。
2. boolean hasMoreTokens() :返回是否还有分隔符。
3. boolean hasMoreElements() :结果同2。
4. String nextToken() :返回从当前位置到下一个分隔符的字符串。
5. Object nextElement() :结果同4。
6. String nextToken(String delim) :与4类似,以指定的分隔符返回结果。
例子:
String s = new String("The Java platform is the ideal platform for network computing");
StringTokenizer st = new StringTokenizer(s);
System.out.println( "Token Total: " + st.countTokens() );
while( st.hasMoreElements() ){
System.out.println( st.nextToken() );
}
结果为:
Token Total: 10
The
Java
platform
is
the
ideal
platform
for
network
computing
例2:
String s = new String("The=Java=platform=is=the=ideal=platform=for=network=computing");
StringTokenizer st = new StringTokenizer(s,"=",true);
System.out.println( "Token Total: " + st.countTokens() );
while( st.hasMoreElements() ){
System.out.println( st.nextToken() );
}
结果为:
Token Total: 19
The
=
Java
=
platform
=
is
=
the
=
ideal
=
platform
=
for
=
network
=
computing
http://yacole.iteye.com/blog/41512
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Java StringTokenizer exampleIn Java, you can StringTokennizer class to split a String into different tokenas by defined delimiter.(space is the default delimiter). Here’re two StringTokennizer examples :
Example 1
Uses StringTokennizer to split a string by “space” and “comma” delimiter, and iterate the StringTokenizer elements and print it out one by one.
package com.mkyong;
import java.util.StringTokenizer;
public class App {
public static void main(String[] args) {
String str = "This is String , split by StringTokenizer, created by mkyong";
StringTokenizer st = new StringTokenizer(str);
System.out.println("---- Split by space ------");
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
System.out.println("---- Split by comma ',' ------");
StringTokenizer st2 = new StringTokenizer(str, ",");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}
Output
---- Split by space ------
This
is
String
,
split
by
StringTokenizer,
created
by
mkyong
---- Split by comma ',' ------
This is String
split by StringTokenizer
created by mkyong
Example 2
Read a csv file and use StringTokenizer to split the string by “|” delimiter, and print it out.
File : c:/test.csv
1| 3.29| mkyong
2| 4.345| eclipse
package com.mkyong;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class ReadFile {
public static void main(String[] args) {
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("c:/test.csv"));
while ((line = br.readLine()) != null) {
System.out.println(line);
StringTokenizer stringTokenizer = new StringTokenizer(line, "|");
while (stringTokenizer.hasMoreElements()) {
Integer id = Integer.parseInt(stringTokenizer.nextElement().toString());
Double price = Double.parseDouble(stringTokenizer.nextElement().toString());
String username = stringTokenizer.nextElement().toString();
StringBuilder sb = new StringBuilder();
sb.append("\nId : " + id);
sb.append("\nPrice : " + price);
sb.append("\nUsername : " + username);
sb.append("\n*******************\n");
System.out.println(sb.toString());
}
}
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Output
1| 3.29| mkyong
Id : 1
Price : 3.29
Username : mkyong
*******************
2| 4.345| eclipse
Id : 2
Price : 4.345
Username : eclipse
*******************
Done
Reference
http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html
http://www.mkyong.com/java/java-stringtokenizer-example/
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------