Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Mastering Java's split() Method: A Practical Guide with Code Examples

What is the split() method in Java?

The String.split() method breaks a string into an array of substrings based on a specified delimiter, which is defined by a regular expression. In most everyday use cases, the delimiter is a space or a comma, but any regex pattern can be used.

Method signature

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

Parameters

Splitting a string with a comma delimiter

For example, suppose you have the string strMain = "Alpha, Beta, Delta, Gamma, Sigma". Splitting on the comma and following space gives five separate words:

Mastering Java s split() Method: A Practical Guide with Code Examples

Use String.split(", ") to perform this operation.

class StrSplit {
  public static void main(String[] args) {
    String strMain = "Alpha, Beta, Delta, Gamma, Sigma";
    String[] arrSplit = strMain.split(", ");
    for (String part : arrSplit) {
      System.out.println(part);
    }
  }
}

Output

Alpha
Beta
Delta
Gamma
Sigma

Using a limit to control the split

Sometimes you only need the first few elements and want the remainder to stay together. The limit parameter lets you do that. With a limit of 3, the third element contains the rest of the string:

  1. Alpha
  2. Beta
  3. Delta, Gamma, Sigma
class StrSplitWithLimit {
  public static void main(String[] args) {
    String strMain = "Alpha, Beta, Delta, Gamma, Sigma";
    String[] arrSplit = strMain.split(", ", 3);
    for (String part : arrSplit) {
      System.out.println(part);
    }
  }
}

Output

Alpha
Beta
Delta, Gamma, Sigma

Splitting a string by spaces

To divide a sentence into individual words, you can split on a single space. For example:

public class SplitBySpace {
  public static void main(String[] args) {
    String strMain = "Welcome to Guru99";
    String[] arrSplit = strMain.split(" ");
    for (String word : arrSplit) {
      System.out.println(word);
    }
  }
}

Output

Welcome
to
Guru99

Java

  1. Master Java String Manipulation: Essential Functions, Methods, and Practical Examples
  2. Java String length() Method: How to Get a String’s Size (Example)
  3. Mastering Java String.indexOf(): Locating Substrings & Practical Examples
  4. Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
  5. Mastering Java's String compareTo() Method: Syntax, Use Cases, and Practical Examples
  6. Java String.contains() Method: How to Check for Substrings – Practical Examples
  7. Mastering Java’s String.endsWith(): How to Check String Suffixes with Examples
  8. Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
  9. Java Interfaces Explained: How to Define and Implement Them with Practical Examples
  10. Reading Files in Java with BufferedReader – A Practical Guide with Examples