NeuroAgent

Java String Split: Complete Guide with Examples

Learn how to split strings in Java using delimiters. Complete guide with examples for basic splitting, delimiter checking, and advanced techniques. Master Java string manipulation today.

Question

How do I split a string in Java using a delimiter?

I want to split a string using a delimiter, for example split “004-034556” into two separate strings by the delimiter “-”:

part1 = “004”;
part2 = “034556”;

That means the first string will contain the characters before ‘-’, and the second string will contain the characters after ‘-’.

I also want to check if the string has the delimiter (‘-’) in it.

NeuroAgent

To split a string in Java using a delimiter, you can use the split() method from the String class, which returns an array of substrings. For your example “004-034556”, calling split("-") will return an array ["004", "034556"], and you can check for the delimiter using the contains() method or by examining the array length.

Contents

Basic String Splitting in Java

The most common way to split a string in Java is using the split() method. This method takes a regular expression as a parameter and returns an array of strings.

java
String input = "004-034556";
String[] parts = input.split("-");

After this code:

  • parts[0] will contain “004”
  • parts[1] will contain “034556”

The split() method uses regular expressions, so special characters need to be escaped if you want to use them literally. For example, to split by a dot, you need to escape it:

java
String input = "file.txt";
String[] parts = input.split("\\."); // Note the double backslash

Checking for Delimiters

Before splitting, you can check if the delimiter exists in the string using several approaches:

Using contains() method

java
String input = "004-034556";
if (input.contains("-")) {
    String[] parts = input.split("-");
    // Process the parts
} else {
    // Handle case where delimiter is not found
}

Using indexOf() method

java
String input = "004-034556";
int delimiterIndex = input.indexOf("-");
if (delimiterIndex != -1) {
    String part1 = input.substring(0, delimiterIndex);
    String part2 = input.substring(delimiterIndex + 1);
    // Process the parts
}

Checking array length after splitting

java
String input = "004-034556";
String[] parts = input.split("-");
if (parts.length > 1) {
    // Delimiter was found
    String part1 = parts[0];
    String part2 = parts[1];
}

Advanced Splitting Techniques

Limiting the number of splits

You can specify a limit parameter to control how many times the splitting should occur:

java
String input = "a-b-c-d";
String[] parts = input.split("-", 2); // Split only twice
// Result: ["a", "b-c-d"]

Splitting with multiple delimiters

To split by multiple different delimiters, you can use a regular expression with the OR operator:

java
String input = "apple,orange;banana";
String[] fruits = input.split("[,;]"); // Split by comma or semicolon

Handling consecutive delimiters

By default, consecutive delimiters create empty strings in the result. You can handle this:

java
String input = "a,,b,c";
String[] parts = input.split(",");
// Result: ["a", "", "b", "c"]

// To skip empty strings, use regex with positive lookbehind
String[] partsNoEmpty = input.split("(?<!^),+");

Handling Special Characters in Delimiters

When your delimiter contains special regex characters, you need to escape them properly:

Delimiter Escape Required Example
. Yes "file.txt".split("\\.")
* Yes "a*b*c".split("\\*")
+ Yes "a+b+c".split("\\+")
? Yes "a?b?c".split("\\?")
` ` Yes
() Yes "a(b)c".split("\\(")
[] Yes "a[b]c".split("\\[")
$ Yes "a$b$c".split("\\$")
\ Yes "a\\b\\c".split("\\\\")

Complete Code Examples

Example 1: Basic splitting with delimiter check

java
public class StringSplitExample {
    public static void main(String[] args) {
        String input = "004-034556";
        
        // Check if delimiter exists
        if (input.contains("-")) {
            String[] parts = input.split("-");
            System.out.println("Part 1: " + parts[0]);
            System.out.println("Part 2: " + parts[1]);
        } else {
            System.out.println("Delimiter not found in the string");
        }
    }
}

Example 2: Robust splitting method

java
public class StringUtils {
    public static String[] splitString(String input, String delimiter) {
        if (input == null || delimiter == null) {
            throw new IllegalArgumentException("Input and delimiter cannot be null");
        }
        
        if (input.isEmpty()) {
            return new String[]{""};
        }
        
        if (!input.contains(delimiter)) {
            return new String[]{input};
        }
        
        return input.split(Pattern.quote(delimiter));
    }
}

Example 3: Handling multiple delimiters

java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MultiDelimiterSplit {
    public static void main(String[] args) {
        String input = "apple,orange;banana|grape";
        
        // Split by multiple delimiters
        String[] fruits = input.split("[,;|]");
        System.out.println("Fruits: " + Arrays.toString(fruits));
        
        // Using Java 8 streams to filter empty strings
        List<String> nonEmptyFruits = Arrays.stream(input.split("[,;|]"))
                                           .filter(s -> !s.isEmpty())
                                           .collect(Collectors.toList());
        System.out.println("Non-empty fruits: " + nonEmptyFruits);
    }
}

Performance Considerations

Regular Expression Compilation

The split() method compiles the delimiter as a regular expression each time it’s called. For performance-critical applications:

java
// Pre-compile the pattern for better performance
Pattern pattern = Pattern.compile("-");
String[] parts = pattern.split(input);

Large Strings

For very large strings, consider using StringTokenizer which can be more memory-efficient:

java
StringTokenizer tokenizer = new StringTokenizer(input, "-");
while (tokenizer.hasMoreTokens()) {
    String part = tokenizer.nextToken();
    // Process each part
}

Memory Usage

Be aware that split() creates a new array and potentially many string objects, which can impact memory usage for large inputs.

Conclusion

  • The String.split() method is the most straightforward way to split strings in Java, using regular expressions as delimiters
  • Always check if your delimiter exists in the string using contains(), indexOf(), or by checking the resulting array length
  • Special regex characters in delimiters need to be properly escaped using Pattern.quote() or manual escaping
  • For better performance with repeated operations, pre-compile your patterns or use StringTokenizer
  • Consider memory implications when splitting very large strings, as split() creates multiple new string objects

For your specific example of splitting “004-034556”, the simplest approach would be:

java
String input = "004-034556";
if (input.contains("-")) {
    String[] parts = input.split("-");
    String part1 = parts[0]; // "004"
    String part2 = parts[1]; // "034556"
}

Sources

  1. Java String.split() Documentation
  2. Java Regular Expressions Tutorial
  3. Java Pattern Class Documentation
  4. StringTokenizer Java Documentation