How can I split string Java efficiently using a delimiter?

How can I split string Java efficiently using a delimiter?

For example, I want to split "004-034556" into two separate strings based on the "-" delimiter:

part1 = "004";
part2 = "034556";

The first string should contain the characters before "-", and the second string should contain the characters after it.

Additionally, I want to check if the string contains the delimiter before performing the split.

The split() method is a simple way to divide a string based on a specified delimiter:

public class StringSplitExample {
    public static void main(String[] args) {
        String str = "004-034556";
        
        if (str.contains("-")) {  // Check if the delimiter exists
            String[] parts = str.split("-");
            String part1 = parts[0];
            String part2 = parts[1];

            System.out.println("Part 1: " + part1);
            System.out.println("Part 2: " + part2);
        } else {
            System.out.println("Delimiter not found.");
        }
    }
}