📜  JavaStringTokenizer和Split方法的区别

📅  最后修改于: 2021-09-13 03:03:20             🧑  作者: Mango

遗留类和接口是在Java的早期版本中形成集合框架的类和接口,以及现在如何重组或重新设计。字符串的拆分基本上是在给定正则表达式的匹配项周围破坏字符串。

字符串可以在Java以多种方式拆分,但最常见的两种方式是使用:

  1. StringTokenizer()
  2. split() 方法

1.字符串分词器

通过获取用于创建 StringTokenizer 对象的字符串的子字符串来返回令牌。 StringTokenizer 对象在内部维护要标记化的字符串的当前位置。

它有 3 个构造函数:

  • StringTokenizer(String str)
  • StringTokenizer(String str, String delimeter)
  • StringTokenizer(String str, String delim, boolean flag)

这里,

  • str:要标记的字符串
  • 分隔符:分隔符来标记字符串(+,/等)
  • flag:决定是否将分隔符视为标记(真/假)
Java
// Java program to demonstrate working of StringTokenizer()
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        String str = "This  is  geek";
        StringTokenizer st = new StringTokenizer(str, " ");
  
        // counting tokens
        System.out.println("Total tokens : "
                           + st.countTokens());
  
        // checking tokens
        for (int i = 0; st.hasMoreTokens(); i++)
            System.out.println("#" + i + ": "
                               + st.nextToken());
    }
}


Java
// Java program to demonstrate split()
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        String str = " This  is  geek";
        String[] split = str.split(" ");
        
        for (int i = 0; i < split.length; i++)
            System.out.println("#" + i + ": " + split[i]);
    }
}


输出
Total tokens : 3
#0: This
#1: is
#2: geek

2. Split()字符串方法

字符串 split() 方法在给定正则表达式的匹配项周围断开给定字符串。

Java有两种 split() 方法的变体:

  • 字符串类方法
public String [ ] split ( String regex, int limit )

Here,
split(): method to split stri
regex:a delimiting regular expression
limit:the result threshold
  • 使用Java.util.regex
public String[] split(String regex) 

Here,
split(): method to split string
regex:a delimiting regular expression
limit: default is 0

Java

// Java program to demonstrate split()
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        String str = " This  is  geek";
        String[] split = str.split(" ");
        
        for (int i = 0; i < split.length; i++)
            System.out.println("#" + i + ": " + split[i]);
    }
}
输出
#0: 
#1: This
#2: 
#3: is
#4: 
#5: geek

JavaStringTokenizer和Split方法的区别

                      StringTokenizer                                       Split()
It is a legacy class that allows an application to break a string into tokens. It is a method of the String class or the java.util.regex package that splits this string around matches of the given regular expression.
It returns one substring at a time. It returns an array of substrings.
It can’t handle empty strings well. It can handle empty strings when you need to parse empty tokens like ant, bat, pat
 It is comparatively less robust  & syntactically fussy. It is more robust & has an easy syntax.
It just accepts a String by which it will split the string It accepts regular expressions.
The delimiter is just a character long. The delimiter is a regular expression.
It is essentially designed for pulling out tokens delimited by fixed substrings. It is essentially designed to parse text data from a source outside your program, like from a file, or from the user.
Because of this restriction, it’s about twice as fast as split(). Slower than StringTokeniser
Consists of a constructor with a parameter that allows you to specify possible delimiter characters. No constructor.