Scanner vs. StringTokenizer vs. String.Split

前端 未结 10 1296
太阳男子
太阳男子 2020-11-22 10:56

I just learned about Java\'s Scanner class and now I\'m wondering how it compares/competes with the StringTokenizer and String.Split. I know that the StringTokenizer and Str

10条回答
  •  一向
    一向 (楼主)
    2020-11-22 11:59

    One important difference is that both String.split() and Scanner can produce empty strings but StringTokenizer never does it.

    For example:

    String str = "ab cd  ef";
    
    StringTokenizer st = new StringTokenizer(str, " ");
    for (int i = 0; st.hasMoreTokens(); i++) System.out.println("#" + i + ": " + st.nextToken());
    
    String[] split = str.split(" ");
    for (int i = 0; i < split.length; i++) System.out.println("#" + i + ": " + split[i]);
    
    Scanner sc = new Scanner(str).useDelimiter(" ");
    for (int i = 0; sc.hasNext(); i++) System.out.println("#" + i + ": " + sc.next());
    

    Output:

    //StringTokenizer
    #0: ab
    #1: cd
    #2: ef
    //String.split()
    #0: ab
    #1: cd
    #2: 
    #3: ef
    //Scanner
    #0: ab
    #1: cd
    #2: 
    #3: ef
    

    This is because the delimiter for String.split() and Scanner.useDelimiter() is not just a string, but a regular expression. We can replace the delimiter " " with " +" in the example above to make them behave like StringTokenizer.

提交回复
热议问题