×

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

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 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.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 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 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.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Java Polymorphism

The process of representing one form in multiple forms is known as Polymorphism. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means...

5 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.