How to split a string in java?

We can split a string in java with the help of String.split(), StringTokenizer, and Pattern.compile() methods.

The most common way to split a string in java is using the split() method. The Java String split() method divides the string at the specified regex and returns an array of substrings.

The syntax of the split() method is-

str.split(String regex, int limit);
  • str is an object of the String class
  • the string is divided at the regex (A regular expression can be a single character or a more complicated pattern).
  • limit is optional and controls the number of resulting substrings.

There are two ways to use the split() method-

split() method -

This java string split method is used when we want the string to be split into a limited number of strings.

If the limit parameter is 0 or negative then the split() method returns an array containing all substrings. If the limit parameter is positive (let's say n) then the split() method returns the maximum of n substrings.

If the limit parameter is not passed, then split() method returns all possible substrings.

If the regular expression passed to split() is invalid, then the split() method raises the PatternSyntaxExpression exception.

Here, we split the string str at "-".

public static void main(String[] args) {
    String str = "one-two-three-four-five";

    // splitting the string at "-"
    // storing the result in an array of strings
    String[] result = str.split("-");

    // converting the array 'result' to string and printing it
    System.out.println("result = " + Arrays.toString(result));
  }

Output-

result = [one, two, three, four, five]

split() With limit Parameter -

The below example shows the usage of different cases of the split() method with limit parameter-

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    String numbers = "one:two:three:four";

    // splitting array at ":"
    String[] result = numbers.split(":");

    // converting the array 'result' to string and printing it
    System.out.println("result = " + Arrays.toString(result));

    // limit is -2; array contins all substrings
    result = numbers.split(":", -2);
    System.out.println("result when limit is -2 = " + Arrays.toString(result));

    // limit is 0; array contains all substrings
    result = numbers.split(":", 0);
    System.out.println("result when limit is 0 = " + Arrays.toString(result));

    // limit is 2; array contains a maximum of 2 substrings
    result = numbers.split(":", 2);
    System.out.println("result when limit is 2 = " + Arrays.toString(result));

    // limit is 3; array contains a maximum of 3 substrings
    result = numbers.split(":", 3);
    System.out.println("result when limit is 3 = " + Arrays.toString(result));

  }
}

Output-

result = [one, two, three, four]  
result when limit is -2 = [one, two, three, four] 
result when limit is 0 = [one, two, three, four]    
result when limit is 2 = [one, two:three:four]    
result when limit is 3 = [one, two, three:four]

If you need to use special characters such as ^, *, \, |, +, etc, then you need to escape these characters. You cannot simply write them. This means that if you want to split at +, then you need to put an escape sequence like this \\+

public static void main(String[] args) {
  String numbers = "1+2+3";

  // splitting the string at "+"
  String[] result = numbers.split("\\+");

  // converting the array 'result' to string and printing it
  System.out.println("result = " + Arrays.toString(result));
}

Output-

result = [1, 2, 3]

Using StringTokenizer -

The string tokenizer in java allows breaking a string into tokens. You can also get the number of tokens inside string object. Keep in mind that the use of StringTokenizer is very uncommon and generally not prefered.

StringTokenizer to split a string by space :

//remember to import "java.util.StringTokenizer"
public static void main(String[] args) {
  String str = "This is a string that will be tokenized on space";

  StringTokenizer defaultTokenizer = new StringTokenizer(str);

  System.out.println("Total number of tokens found are " + defaultTokenizer.countTokens());

  while (defaultTokenizer.hasMoreTokens()) {
    System.out.println(defaultTokenizer.nextToken());
  }
}

Output-

Total number of tokens found are 10
This
is
a
string
that
will
be
tokenized
on
space

To split a string by ’$’ or any other special character using StringTokenizer :

//remember to import "java.util.StringTokenizer"
public static void main(String[] args) {
  String str = "This is a string that $will be tokenized on dollar";

  StringTokenizer defaultTokenizer = new StringTokenizer(str,"$");

  System.out.println("Total number of tokens found are " + defaultTokenizer.countTokens());

  while (defaultTokenizer.hasMoreTokens()) {
    System.out.println(defaultTokenizer.nextToken());
  }
}

Output-

This is a string that 
will be tokenized on dollar 
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions