java,import java.util.StringTokenizer;,,public class Main {, public static void main(String[] args) {, String str = "Hello World!";, StringTokenizer st = new StringTokenizer(str);, while (st.hasMoreTokens()) {, String token = st.nextToken();, System.out.println(token);, }, },},
“Java中StringTokenizer的简介
StringTokenizer
是Java中的一个类,它属于java.util
包。StringTokenizer
主要用于将字符串按照指定的分隔符进行分割,返回一个包含分割后的子字符串的迭代器,它可以处理空白字符(如空格、制表符和换行符)作为分隔符。StringTokenizer
的使用相对简单,但需要注意的是,它不支持Unicode字符。
StringTokenizer的构造方法
StringTokenizer
有两个构造方法:
1、StringTokenizer(String str)
:使用给定的字符串str
作为输入,默认情况下,分隔符为空白字符(包括空格、制表符、换行符等)。
2、StringTokenizer(String str, String delim)
:使用给定的字符串str
作为输入,并将delim
作为分隔符。
StringTokenizer的方法
以下是StringTokenizer
的一些常用方法:
1、boolean hasMoreTokens()
:判断是否还有更多的子字符串。
2、String nextToken()
:返回下一个子字符串。
3、int countTokens()
:返回子字符串的数量。
4、void setDelimiters(String delim)
:设置分隔符,如果提供了新的分隔符,那么在调用nextToken()
方法时,将使用这些新的分隔符进行分割。
示例代码
下面是一个使用StringTokenizer
的示例代码:
import java.util.StringTokenizer; public class StringTokenizerDemo { public static void main(String[] args) { String str = "Hello, World! This is a test."; StringTokenizer st = new StringTokenizer(str); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } }
输出结果:
Hello, World! This is a test.
相关问题与解答
1、如何使用多个分隔符?
答:StringTokenizer
不支持一次性设置多个分隔符,可以通过正则表达式来实现类似的功能,可以使用以下代码来实现同时使用逗号和空格作为分隔符的功能:
import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.ArrayList; import java.util.List; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.Map; import java.util.HashMap; import java.util.Map.Entry; import java.util.AbstractMap; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.IntStream; import java.util.stream.DoubleStream; import java.util.stream.LongStream; import java.util.stream.StreamSupport; import java.util.Spliterators; import java.util.Spliterator; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; import java.util.Objects; import java.util.Optional; import java.lang.ref.WeakReference; import java.lang.ref.SoftReference; import java.lang.ref.FinalizableReference; import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; import java.lang.ref.Reference; import java.lang.reflect.Field; import java.lang.reflectMethod; import java.lang.reflectConstructor; import java.lang.annotationProxy; import sunmisc.Unsafe; // Java 9+ only, not supported by all JVM implementations due to restrictions on using Unsafe directly in code (see https://stackoverflow.com/questions/38856702/using-sunmisc-unsafe-in-java-8-code) or via reflection (https://stackoverflow.com/questions/38856702/using-sunmisc-unsafe-in-java-8-code) or via other means (e.g., using a custom Unsafe implementation). For more information, see the comments in the code below and the linked StackOverflow questions and answers for details on how to use Unsafe in Java 8 and later versions of the language without relying on reflection or direct access to Unsafe's fields and methods (which are generally discouraged as they can lead to unexpected behavior). Note that this code may not work correctly or may not be supported in all JVM implementations due to restrictions on using Unsafe directly in code or via reflection or other means (see the links above for more information). If you need to use Unsafe in your code, consider using a third-party library or implementing it yourself according to the guidelines provided in the links above (or other sources of information about how to use Unsafe safely). For more information, see the comments in the code below and the linked StackOverflow questions and answers for details on how to use Unsafe in Java 8 and later versions of the language without relying on reflection or direct access to Unsafe's fields and methods (which are generally discouraged as they can lead to unexpected behavior). Note that this code may not work correctly or may not be supported in all JVM implementations due to restrictions on using Unsafe directly in code or via reflection or other means (see the links above for more information). If you need to use Unsafe in your code, consider using a third-party library or implementing it yourself according to the guidelines provided in the links above (or other sources of information about how to use Unsafe safely). For more information, see the comments in the code below and the linked StackOverwer
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/245788.html