×

Java String split() method

Java String split() method split current String against given regular expression and returns a char array.

Syntax:

public String split(String regex)                
public String split(String regex, int limit)

Parameters:

regex : it is a regular expression to be applied on String.

limit : it is limit for the number of Strings in array. If it is zero , it will return all String matching regex.

Returns:

array of Strings

Throws:

PatternSyntaxException if pattern for regex is invalid.

Java String split() method 1 example:

public class JavaStringSplitEx1
    { 
       public static void main(String args[])
       { 
         String s1="Welcome to the Tutorial and example"; 
         String[] words=s1.split("\\s");//splits the string based on whitespace 
    //using java foreach loop to print elements of string array 
        for(String w:words){ 
          System.out.println(w); 
         } 
       }
    }

Output:

Welcome
to
the
Tutorial
and
example

Java String split() method with regex and length example 2

    public class JavaStringSplitEx2
    { 
    public static void main(String args[])
    { 
    String s1="welcome to tutorialsandexample world"; 
    System.out.println("returning words:"); 
    for(String w:s1.split("\\s",0)){ 
    System.out.println(w); 
    } 
        System.out.println(""); 
    System.out.println("returning words:"); 
    for(String w:s1.split("\\s",1)){ 
    System.out.println(w); 
    } 
       System.out.println(""); 
    System.out.println("returning words:"); 
    for(String w:s1.split("\\s",2)){ 
    System.out.println(w); 
    } 
    }      
    }

Output:

returning words:
welcome
to
tutorialsandexample
world
returning words:
welcome to tutorialsandexample world
returning words:
welcome
to tutorialsandexample world

Java String split() method with regex and length example 3

    public class JavaStringSplitEx3 { 
        public static void main(String[] args) { 
            String str = "Tutorialand Example"; 
            System.out.println("Returning words:"); 
            String[] arr = str.split("t", 0); 
            for (String w : arr) { 
                System.out.println(w); 
            } 
            System.out.println("Split array length: "+arr.length); 
        } 
    }

Output:

Returning words:
Tu
orialand Example
Split array length: 2

Java String split() method with regex and limit example 4

 public class JavaStringSplitEx4
{
    public static void main(String args[])
    {
        String str = "Tutorial&&And&&Example";
        String [] arrOfStr = str.split("&", 5);
        for (String a : arrOfStr)
            System.out.println(a);
    }
}

Output:

Tutorial
And
Example

Java String split() method example 5

public class JavaStringSplitEx5
{
    public static void main(String args[])
    {
        String str = "Tutorial&&And&&Example";
        String [] arrOfStr = str.split("&",-2);
        for (String a : arrOfStr)
            System.out.println(a);
    }
}

Output:

Tutorial
And
Example

Related Topics

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Why main method is static in Java

The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most...

4 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Java Strictfp Keyword

Strictfp is used to impose limits on floating-point calculation. It ensures that we will get the same result on every platform while performing an operation with the floating-point variable. The floating-point calculation is platform-dependent due...

1 minute read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.