Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

String split() in Java

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Java is one of the most widely used programming languages in the world. It has predefined data structures used to store data. For example, ArrayList, Strings, etc. A string in Java is defined as an object that represents a sequence of char values. The Java string class provides many predefined methods that allow us to perform various operations on strings. One such method is split(), which is used to split the string. In this blog, we will discuss string split ().

 

source

About String split()

String split() is used to split the string at the specified regular expression. We define the regex in the regex parameter of the split(). Split() also has a limit parameter that controls the number of substrings returned.

string.split()

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Parameters of String split()

There are two parameters of string split():

String regex: It is the regular expression that we give. The string is divided at this regex.

Int limit: It gives us the number of the resulting substrings. It is optional, and if not given, string split() will return all the possible substrings of the string divided by the regex. If the limit is 0, it will return all the possible resultant substring.

Syntax of String split()

The syntax of string split() is:

If limit is not given

string.split(string regex)

If limit is given

string.split(string regex, int limit)

Return value of String split()

After splitting the string around the given regex, it returns the array of strings that we get after splitting the string. The number of substrings is decided by the limit parameter. If no limit is given, it will return all the possible resultant substrings.

Implementation of string split() without limit

import java.io.*;

class Main {

  public static void main(String[] args) {

    // initializing the string
    String text = "Welcome to coding-ninjas";

    // split string from the given regex
    String[] result1 = text.split(" ");
    String[] result2 = text.split("-");

    //printing the result
    System.out.print("result1 = ");
    for (String str : result1) {
      System.out.print(str + ", ");
    }
    
    System.out.println();
    
    System.out.print("result2 = ");
    for (String str : result2) {
      System.out.print(str + ", ");
    }
  }
}

 

Output:

result1 = Welcome, to, coding-ninjas, 
result2 = Welcome to coding, ninjas, 

Implementation of string split() with limit

import java.io.*;

class Main {

  public static void main(String[] args) {

    // initializing the string
    String text = "Coding ninjas Coding Ninjas Studio provides a complete preparation guide for coding interviews";

    // split string from the given regex and providing the limit
    String[] result1 = text.split(" ", 5);
    String[] result2 = text.split(" ", 11);

    //printing the result
    System.out.print("result1 = ");
    for (String str : result1) {
      System.out.print(str + ", ");
    }
    
    System.out.println();
    
    System.out.print("result2 = ");
    for (String str : result2) {
      System.out.print(str + ", ");
    }
  }
}

 

Output:

result1 = Coding, ninjas, Coding Ninjas Studio, provides, a complete preparation guide for coding interviews, 
result2 = Coding, ninjas, Coding Ninjas Studio, provides, a, complete, preparation, guide, for, coding, interviews, 

Frequently Asked Questions

What are some of the predefined methods in the Java string class?

CharAt(), startsWith(), equals(), trim() are some of the predefined methods in Java string class.

What if the provided regex is not present in the string?

The whole string is returned as it is if the regex is not present in the string.

What are some of the use cases of string split() in real life?

String split can be used to separate the parts of an HTTP request header, separate parts of a name, get the phone numbers, etc.

Conclusion

This blog talked about the string split() method. We also implemented it in Java. You must be eager to learn more about the Java string class. Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.
 

Recommended problems -

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Topics covered
1.
Introduction
2.
About String split()
3.
Parameters of String split()
4.
Syntax of String split()
4.1.
If limit is not given
4.2.
If limit is given
5.
Return value of String split()
6.
Implementation of string split() without limit
7.
Implementation of string split() with limit
8.
Frequently Asked Questions
8.1.
What are some of the predefined methods in the Java string class?
8.2.
What if the provided regex is not present in the string?
8.3.
What are some of the use cases of string split() in real life?
9.
Conclusion