java

Split a String in Java

In this tutorial, we are going to see how to split a String in Java. The String.split() method splits a String according to a given regular expression.
 

Syntax

The syntax of the split() method:

public String[] split(String regex)

 

Parameters

regex – regular expression.
 

Return value

It returns an array of type String by dividing the String according to the given regular expression.
 

 

How to Split a String in Java
import java.io.*;

public class SplitExemple {

   public static void main(String args[]) {
      String Str = new String("Hello-World");     
      
      for (String s: Str.split("-")) {
         System.out.println(s);
      }
   }
}

Output:

Hello
World
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *