Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

When working with strings in Java, we may encounter scenarios where we need to remove all characters before a particular delimiter or character. Fortunately, we can accomplish this task using various techniques in Java, such as traditional looping, string manipulation methods, or regular expressions.

In this tutorial, we’ll explore several approaches to remove all characters before a specified character in a string.

2. Using Indexing and Substrings

One straightforward approach to removing all characters before a specific character involves finding the index of the desired character and then using the substring() method to extract the substring starting from that index.

Here’s a simple example:

String inputString = "Hello World!";
char targetCharacter = 'W';

@Test
public void givenString_whenUsingSubstring_thenCharactersRemoved() {
    int index = inputString.indexOf(targetCharacter);
    if (index != -1) {
        String result = inputString.substring(index);
        assertEquals("World!", result);
    } else {
        assertEquals(inputString, inputString);
    }
}

In this example, we initialize a String named inputString and a target character targetChar. We first find the index of the targetChar using the indexOf() method. If the targetChar is found (index != -1), it extracts the substring starting from the index using substring(). Then we validate the result string using assertEquals() to ensure it matches the expected value (World!). Otherwise, it returns the original string.

3. Using Regular Expressions

Another approach involves using regular expressions (regex) to replace all characters before the specified character with an empty string.

Let’s check a simple implementation:

@Test
public void givenString_whenUsingRegex_thenCharactersRemoved() {
    int index = inputString.indexOf(targetCharacter);
    if (index != -1) {
        String result = targetCharacter + inputString.replaceAll(".*" + targetCharacter, "");
        assertEquals("World!", result);
    } else {
        assertEquals(inputString, inputString);
    }
}

Here, we utilize the replaceAll() with a regex pattern that matches any sequence of characters (.*) followed by the target character. Moreover, we prepend the targetCharacter at the beginning of the replacement string, ensuring that it’s included in the final result.

This pattern effectively removes all characters till the occurrence of the target character in the string.

4. Using StringBuilder

We can also leverage StringBuilder to remove all characters before a specific character by locating the target character and then manipulating the string as follows:

@Test
public void givenString_whenUsingStringBuilder_thenCharactersRemoved() {
    StringBuilder sb = new StringBuilder(inputString);
    int index = sb.indexOf(String.valueOf(targetCharacter));
    if (index != -1) {
        sb.delete(0, index);
        assertEquals("World!", sb.toString());
    } else {
        assertEquals(inputString, inputString);
    }
}

In this implementation, we first define a StringBuilder object. Moreover, we utilize the indexOf() method to find the index of the target character. Finally, we delete all characters before that index using the delete() method.

5. Conclusion

In conclusion, removing all characters before a specific character in a string is a common task in string manipulation. Hence, we explored several methods to achieve this in Java, including using indexing and substring extraction, regular expressions, and StringBuilder.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1 Comment
Oldest
Newest