×

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

Java Integer toString() method

The toString() method of Java Integer class returns a String object which represents this Integer’s value. The second syntax returns a String object which represents the specified integer. The third syntax returns...

2 minutes read.

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

How to Calculate Time Difference Between Two Dates in Java?

Date is being used extensively in Java to calculate date differences. While constructing an application, the date of joining an organisation, admission date, appointment date, and others might be included....

4 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Java finally

Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection...

5 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.