下面的代码简单演示了java中字符串分割函数split的用法
语法如下
public String[] split(String regex, int limit)
or
public String[] split(String regex)
范例演示
import java.io.*; public class Test{ public static void main(String args[]){ String Str = new String("Welcome-to-Tutorialspoint.com"); System.out.println("Return Value :" ); for (String retval: Str.split("-", 2)){ System.out.println(retval); } System.out.println(""); System.out.println("Return Value :" ); for (String retval: Str.split("-", 3)){ System.out.println(retval); } System.out.println(""); System.out.println("Return Value :" ); for (String retval: Str.split("-", 0)){ System.out.println(retval); } System.out.println(""); System.out.println("Return Value :" ); for (String retval: Str.split("-")){ System.out.println(retval); } } }
返回结果如下
Return Value : Welcome to-Tutorialspoint.com Return Value : Welcome to Tutorialspoint.com Return Value: Welcome to Tutorialspoint.com Return Value : Welcome to Tutorialspoint.com