Easy Tutorial
❮ Java Documentation Java String Endswith ❯

Java Example - String Splitting

Java Examples

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

JavaStringSplitEmp.java File

public class JavaStringSplitEmp {
   public static void main(String args[]){

      String str = "www-tutorialpro.org";
      String[] temp;
      String delimeter = "-";  // Specify the delimiter character
      temp = str.split(delimeter); // Split the string
      // Regular 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.tutorialpro.org";
      String[] temp1;
      String delimeter1 = "\\.";  // Specify the delimiter character, dot needs to be escaped
      temp1 = str1.split(delimeter1); // Split the string
      for(String x :  temp1){
         System.out.println(x);
         System.out.println("");
      }
   }
}

The above code example outputs:

www

tutorialpro

org

------Java for-each loop output method-----
www

tutorialpro

org

Java Examples

❮ Java Documentation Java String Endswith ❯