How to get a part of string after a specified character in JavaScript?

To get a part of a string after a specified character in JavaScript, you can use the substring() method combined with indexOf(). This technique allows you to extract portions of text before or after any character.

Understanding substring()

The substring() method extracts characters from a string between two specified indices. It takes a start index (inclusive) and an optional end index (exclusive).

Syntax for Getting Text After a Character

string.substring(string.indexOf(character) + 1);

Here, indexOf(character) finds the position of the character, and adding + 1 starts extraction from the next position.

Syntax for Getting Text Before a Character

string.substring(0, string.indexOf(character));

This extracts from the beginning (index 0) up to the character position.

Example: Extracting Text After a Character

<html>
<body>
<script>
    let email = "user@domain.com";
    let domain = email.substring(email.indexOf('@') + 1);
    document.write("Domain: " + domain);
    
    document.write("<br><br>");
    
    let path = "home/user/documents";
    let afterSlash = path.substring(path.indexOf('/') + 1);
    document.write("After first slash: " + afterSlash);
</script>
</body>
</html>
Domain: domain.com

After first slash: user/documents

Example: Extracting Text Before a Character

<html>
<body>
<script>
    let email = "user@domain.com";
    let username = email.substring(0, email.indexOf('@'));
    document.write("Username: " + username);
    
    document.write("<br><br>");
    
    let filename = "document.pdf";
    let name = filename.substring(0, filename.indexOf('.'));
    document.write("File name: " + name);
</script>
</body>
</html>
Username: user

File name: document

Complete Function Example

<html>
<body>
<script>
    function getStringPart(string, character, position) {
        if (position === 'after') {
            return string.substring(string.indexOf(character) + 1);
        } else if (position === 'before') {
            return string.substring(0, string.indexOf(character));
        } else {
            return string;
        }
    }
    
    let text = "JavaScript-Tutorial";
    
    document.write("Original: " + text);
    document.write("<br>");
    document.write("Before '-': " + getStringPart(text, '-', 'before'));
    document.write("<br>");
    document.write("After '-': " + getStringPart(text, '-', 'after'));
</script>
</body>
</html>
Original: JavaScript-Tutorial
Before '-': JavaScript
After '-': Tutorial

Key Points

  • indexOf() returns -1 if the character is not found
  • Always check if the character exists before using substring()
  • substring() automatically handles invalid indices
  • For the last occurrence of a character, use lastIndexOf() instead

Conclusion

Using substring() with indexOf() provides a reliable way to extract text before or after any specified character. This technique is commonly used for parsing strings like URLs, file paths, and email addresses.

Updated on: 2026-03-15T23:18:59+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements