Java 实例 - 字符串分隔(StringTokenizer)
Java 教程
· 2019-02-10 08:14:05
Java 中我们可以使用 StringTokennizer 设置不同分隔符来分隔字符串,默认的分隔符是:空格、制表符(\t)、换行符(\n)、回车符(\r)。
以下实例演示了 StringTokennizer 使用空格和等号来分隔字符串:
更多 StringTokennizer 介绍可以查看:Java StringTokenizer 类使用方法
JavaStringSplitEmp.java 文件
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
String str = "This is String , split by StringTokenizer, created by codercto";
StringTokenizer st = new StringTokenizer(str);
System.out.println("----- 通过空格分隔 ------");
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
System.out.println("----- 通过逗号分隔 ------");
StringTokenizer st2 = new StringTokenizer(str, ",");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}
输出结果:
----- 通过空格分隔 ------ This is String , split by StringTokenizer, created by codercto ----- 通过等号分隔 ------ This is String split by StringTokenizer created by codercto
点击查看所有 Java 教程 文章: https://codercto.com/courses/l/12.html
The Creative Curve
Allen Gannett / Knopf Doubleday Publishing Group / 2018-6-12
Big data entrepreneur Allen Gannett overturns the mythology around creative genius, and reveals the science and secrets behind achieving breakout commercial success in any field. We have been s......一起来看看 《The Creative Curve》 这本书的介绍吧!