java tutorial - Java split() method - java programming - learn java - java basics - java for beginners



Java strings split

Learn Java - Java tutorial - Java strings split - Java examples - Java programs

Description

The split () method — in Java, divides the given line around this regular expression and has two options

Syntax

Syntax method:

public String[] split(String regex, int limit)
or
public String[] split(String regex)
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • regex - the delimitation of a regular expression;
  • limit is a threshold, the result of which means how many lines must be returned.

Return value

  • In Java, split () returns an array of strings calculated by dividing the given line around this regular expression.

Example 1: Split a string around a regular expression

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome-to-wikitechy.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("");
   }
}
click below button to copy the code. By - java tutorial - team

Output

Return value: 
Return Value :
Welcome
to-wikitechy.com

Return Value :
Welcome
to
wikitechy.com

Return Value :
Welcome
to
wikitechy.com

Example 2: Split a string into words

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome-to-wikitechy.com");
      System.out.println("Return Value :" );      
      
      for (String retval: Str.split("-")) {
         System.out.println(retval);
      }
   }
}
click below button to copy the code. By - java tutorial - team

Output

Return Value :
Welcome
to
wikitechy.com

Related Searches to Java split() method