Split a string in Java

Java 8Object Oriented ProgrammingProgramming

Let’s say the following is our string.

String str = "This,is,it";

We will now see how to split a string using the split() method. Include the delimiter as the parameter.

String[] strSplit = str.split(",");

Add the result in an array and display it.

The following is an example.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      String str = "This,is,it";
      System.out.println("String: "+str);
      String[] strSplit = str.split(",");
      System.out.println("Splitting String...");
      for (int i = 0; i < strSplit.length; i++)
System.out.println(strSplit[i]);
}
}

Output

String: This,is,it
Splitting String...
This
is
it
raja
Published on 17-Dec-2018 11:28:14
Advertisements