Java Examples - String Split

Java 实例Java Examples

The following example uses the split(string) method to split the string into an array by specifying a delimiter:

JavaStringSplitEmp.java file

public class JavaStringSplitEmp { public static void main(String args[]){ String str = "www-example-com"; String[] temp; String delimeter = "-"; //Specify the split character temp = str.split(delimeter); //Split string //Ordinary for loop for(int i =0; i < temp.length ; i++){ System.out.println(temp[i]); System.out.println(""); } System.out.println("------java for each loop output method-----"); String str1 = "www.example.com"; String[] temp1; String delimeter1 = "\\."; //Specify the split character, the . needs to be escaped temp1 = str1.split(delimeter1); //Split string for(String x : temp1){ System.out.println(x); System.out.println(""); } } }

The output result of the above code example is:

www

example

com

------java for each循环输出的方法-----
www

example

com

Java 实例Java Examples

Other extensions