Java 使用 StringTokenizer 和 String.split() 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19677919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:19:53  来源:igfitidea点击:

Difference between using StringTokenizer and String.split( )?

javastringsplitstringtokenizer

提问by Satyendra

I've been using String[] split(String)of Stringclass to split any string for some given delimiter, and it worked fine.

我已经使用过String[] split(String)String阶级分裂任何字符串为某些给定的分隔符,它工作得很好。

However, now it is expected to re-factor the same logic with StringTokenizer. But what are the differences and benifits of using one over the other.

但是,现在预计将使用StringTokenizer. 但是使用其中一种的区别和好处是什么。

Also, I feel that String[]returned by split()in a single call is much efficient option than using the objects of the class StringTokenizer.

此外,我觉得在单个调用中String[]返回split()比使用类的对象更有效的选择StringTokenizer

回答by MadProgrammer

Take a look at the JavaDocs

看一下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]);

StringTokenizer 是一个遗留类,出于兼容性原因保留,但不鼓励在新代码中使用它。建议任何寻求此功能的人改用 String 的 split 方法或 java.util.regex 包。

以下示例说明了如何使用 String.split 方法将字符串分解为其基本标记:

 String[] result = "this is a test".split("\s");
 for (int x=0; x<result.length; x++)
     System.out.println(result[x]);

回答by Petr Mensik

String#splitaccepts a regular expression whether StringTokenizerjust accepts a Stringby which will split the string. You should always stick to the String#split, it's more robust then StringTokenizer.

String#split接受一个正则表达式,是否StringTokenizer只接受一个String将拆分字符串的依据。您应该始终坚持使用String#split,它比 StringTokenizer 更强大。

回答by Ankit Rustagi

Read this

这个

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.

StringTokenizer 是一个遗留类,出于兼容性原因保留,但不鼓励在新代码中使用它。建议任何寻求此功能的人改用 String 的 split 方法或 java.util.regex 包。

回答by koljaTM

http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.htmlsay:

http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html说:

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.

StringTokenizer 是一个遗留类,出于兼容性原因保留,但不鼓励在新代码中使用它。建议任何寻求此功能的人改用 String 的 split 方法或 java.util.regex 包。

So I would say, don't change it and show that line to the person who recommended refactoring it. Maybe they have old information or another good reason to tell you.

所以我会说,不要改变它并向推荐重构它的人展示那条线。也许他们有旧信息或其他很好的理由告诉你。

回答by user2114253

I have the following program,

我有以下程序,

The string "x" is a tab separated 12s34;

字符串 "x" 是一个以 12s34 分隔的制表符;

    public class Testoken {
        public static void main(String[] args) {
            String x = "1   2   s       3           4   ";
            StringTokenizer st = new StringTokenizer(x,"\t");
            int i = 0;
            while(st.hasMoreTokens()){
                System.out.println("token-->"+st.nextToken());            
                i++;
            }
            System.out.println("i-->"+i);//elements from tokenizer
            String [] a = x.split("\t");
            System.out.println("length--->"+a.length);
            for(int y = 0;y<a.length;y++){
            System.out.println("value-->"+a[y]);//elements from split
            }
        }
    }   





Output: 

token-->1 
token-->2 
token-->s 
token-->3 
token-->4 
i-->5 
length--->8 
value-->1 
value-->2 
value-->s 
value--> 
value-->3 
value--> 
value--> 
value-->4