<分区>
我一直在使用 String
类的 String[] split(String)
为某个给定的定界符拆分任何字符串,并且效果很好。
但是,现在期望用 StringTokenizer
重构相同的逻辑。但是使用其中一个的区别和好处是什么。
此外,我觉得 split()
在一次调用中返回的 String[]
比使用类 StringTokenizer< 的对象更有效
.
<分区>
我一直在使用 String
类的 String[] split(String)
为某个给定的定界符拆分任何字符串,并且效果很好。
但是,现在期望用 StringTokenizer
重构相同的逻辑。但是使用其中一个的区别和好处是什么。
此外,我觉得 split()
在一次调用中返回的 String[]
比使用类 StringTokenizer< 的对象更有效
.
最佳答案
看看 JavaDocs
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
The following example illustrates how the String.split method can be used to break up a string into its basic tokens:
String[] result = "this is a test".split("\\s"); for (int x=0; x<result.length; x++) System.out.println(result[x]);
关于java - 使用 StringTokenizer 和 String.split( ) 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19677919/