Java String split() 方法示例


Java String split() 方法示例

import java.lang.*;

public class StringDemo {

   public static void main(String[] args) {

      String str = "a d, m, i.n";

      String delimiters = "\\s+|,\\s*|\\.\\s*";



      //analysing the string

      String[] tokensVal = str.split(delimiters);



      //prints the count of tokens

      System.out.println("Count of tokens = " + tokensVal.length);



      for(String token : tokensVal) {

         System.out.print(token);

      }

      //analysing the string with limit as 3

      tokensVal = str.split(delimiters, 3);



      //prints the count of tokens

      System.out.println("\nCount of tokens = " + tokensVal.length);

      for(String token : tokensVal) {

         System.out.print(token);

      }

   }

}