How to Split a String between Letters and Digits in Java

In this article, we'll examine how to split a string between letters and digits in Java. Splitting a string into numeric, alphabetic, and special symbol components entails classifying each character based on type.

Example:

Input: Hello123@World

Output:

Numeric characters: "123"

Alphabetic characters: "HelloWorld"

Special symbols: "@"

Explanation: As we examine the input string "Hello123@World," we see digits that are classified as numeric characters ("123"). Alphabetic characters ("HelloWorld"), as well as special symbols like "@", are recognized individually.

Approach: Iterative [Predefined Functions]

This Java code divides characters in a string into numerical, alphabetic, and special symbol components. It iterates through the string with a typical for loop, using Character.isDigit() and Character.isLetter() to identify numeric and alphabetic characters, respectively. Special symbols are created by eliminating characters that meet the numeric or alphabetic requirements. The findings are kept in independent StringBuilder objects, allowing for further analysis or processing.

FileName: StringSplitter.java

public class StringSplitter {

    public static void main(String[] args) {

        String inputString = "Hello123@World";

        StringBuilder numericChars = new StringBuilder();

        StringBuilder alphabeticChars = new StringBuilder();

        StringBuilder specialChars = new StringBuilder();

        for (int i = 0; i < inputString.length(); i++) {

            char c = inputString.charAt(i);

            if (Character.isDigit(c)) {

                numericChars.append(c);

            } else if (Character.isLetter(c)) {

                alphabeticChars.append(c);

            } else {

                specialChars.append(c);

            }

        }

        System.out.println("Numeric characters: " + numericChars.toString());

        System.out.println("Alphabetic characters: " + alphabeticChars.toString());

        System.out.println("Special symbols: " + specialChars.toString());

    }

}

Output:

Numeric characters: 123

Alphabetic characters: HelloWorld

Special symbols: @

Complexity Analysis: The provided Java code has a time and space complexity of O(n), where n is the length of the input string.

Approach: Iterative [Without Predefined Functions]

The code uses an iterative approach, traversing each character in the input string with a for loop. Custom methods isNumericCharacter and isAlphabeticCharacter are defined to identify whether a character is numeric or alphabetic. The isNumericCharacter method determines whether a given character is a numeric digit by comparing its ASCII value to the range of digits ('0' to '9'). If the criteria are met, the procedure returns true, denoting a numeric character; otherwise, it returns false. The isAlphabeticCharacter method does the same thing, determining whether a character is an alphabetic letter by comparing its ASCII value to the ranges of capital and lowercase letters ('A' to 'Z' or 'a' to 'z').

FileName: StringSplitter1.java

public class StringSplitter1 {

    public static void main(String[] args) {

        String inputString = "Hello123@World";

        StringBuilder numericChars = new StringBuilder();

        StringBuilder alphabeticChars = new StringBuilder();

        StringBuilder specialChars = new StringBuilder();

        for (int i = 0; i < inputString.length(); i++) {

            char c = inputString.charAt(i);

            if (isNumericCharacter(c)) {

                numericChars.append(c);

            } else if (isAlphabeticCharacter(c)) {

                alphabeticChars.append(c);

            } else {

                specialChars.append(c);

            }

        }

        System.out.println("Numeric characters: " + numericChars.toString());

        System.out.println("Alphabetic characters: " + alphabeticChars.toString());

        System.out.println("Special symbols: " + specialChars.toString());

    }

    private static boolean isNumericCharacter(char c) {

        return c >= '0' && c <= '9';

    }

    private static boolean isAlphabeticCharacter(char c) {

        return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');

    }

}

Output:

Numeric characters: 123

Alphabetic characters: HelloWorld

Special symbols: @

Complexity Analysis: The provided code's time complexity is O(n), where n represents the length of the input string. The code iterates through each character once, and the operations in the loop are constant in time. The space complexity is additionally O(n) since StringBuilder objects are utilized to store numeric, alphabetic, and special symbol components.

Approach: Regex-Based String Splitting

The split method utilizes the regex pattern (?<=\\D)(?=\\d): Positive lookbehind for non-digits, followed by positive lookahead for digits. |: OR.(?<=\\d)(?=\\D): Positive lookbehind for a digit and positive lookahead for a non-digit. |: OR.(?<=\\W)(?=\\w): A positive lookback for a non-word character (special character), followed by a positive lookahead for a word character.

FileName: StringSplitter3.java

public class StringSplitter3 {

    public static void main(String[] args) {

        String inputString = "Hello123@World";

        // Use regular expression to split between numbers, letters, and special characters

        String[] parts = inputString.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)|(?<=\\W)(?=\\w)");

        String numericChars = parts[0];

        String alphabeticChars = parts[1];

        String specialChars = parts[2];

        System.out.println("Numeric characters: " + numericChars);

        System.out.println("Alphabetic characters: " + alphabeticChars);

        System.out.println("Special characters: " + specialChars);

    }

}

Output:

Numeric characters: 123

Alphabetic characters: HelloWorld

Special symbols: @

Complexity Analysis: The code has a time complexity of O(n), where n is the length of the input string because it uses the split technique with a rather simple regular expression. The worst-case space complexity is O(n), as it generates an array of substrings proportional to the input size.