Java split() Method

Java String类Java String Class


The split() method splits the string according to the given regular expression.

Note: . 、 $、 |and*Escape characters, etc., must be added.\\。

Note:For multiple delimiters, you can use|as a hyphen.

Syntax

public String[] split(String regex, int limit)

Parameters

  • regex-- Regular expression delimiter.

  • limit-- The number of parts to split.

Return Value

String array.

Example

Example

public class Test { public static void main(String args[]) { String str = new String("Welcome-to-Example"); System.out.println("- Delimiter return value:" ); for (String retval: str.split("-")){ System.out.println(retval); } System.out.println(""); System.out.println("- Delimiter with split limit return value:" ); for (String retval: str.split("-", 2)){ System.out.println(retval); } System.out.println(""); String str2 = new String("www.example.com"); System.out.println("- Escape character return value:" ); for (String retval: str2.split("\\.", 3)){ System.out.println(retval); } System.out.println(""); String str3 = new String("acount=? and uu =? or n=?"); System.out.println("- Multiple delimiters return value:" ); for (String retval: str3.split("and|or")){ System.out.println(retval); } } }

The result of executing the above program is:

- 分隔符返回值 :
Welcome
to
Example

- 分隔符设置分割份数返回值 :
Welcome
to-Example

转义字符返回值 :
www
example
com

多个分隔符返回值 :
acount=? 
 uu =? 
 n=?

Java String类Java String Class

Other Extensions