×

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 divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute 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.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Java String charAt() method

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1. Syntax: public char charAt (int index) Parmeters It accepts only...

3 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Java Integer remainder Unsigned() method

The remainderUnsigned() method of Java Integer class returns the unsigned remainder by dividing the first and second argument. Syntax public static int remainderUnsigned(int dividend, int divisor)  Parameters The ‘dividend’ and ‘divisor’ represents the value...

1 minute read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Can we override Private Method in Java

In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not. Override in Java Any...

3 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.