Replace Space with Underscore in Java [2 ways]

In this post, I will be sharing how to replace space with an underscore in Java. There are two ways to achieve our goal of replacing space with an underscore in Java:

1. Using replace() method

2. Using replaceAll() method

Read Also: Java String replace(), replaceAll() and replaceFirst() Method Example

Let's dive deep into the topic:

Replace Space with Underscore in Java (Examples)

1. Using replace() method


We can easily use replace() method to replace space with an underscore in Java as shown below in the examples.  First, we will understand the syntax of replace() method in Java:

Syntax of replace() method


public String replace(CharSequence target,
                      CharSequence replacement)

According to Oracle docs, replace() method replaces each substring of the given string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaaaa" will result in "bba" rather than "abb".

public class ReplaceSpaceWithUnderscore {
    public static void main(String args[]) {
        String str = "Alive is Awesome";
        str = str.replace(" ","_");
        System.out.println(str);
    }
}


Output:
Alive_is_Awesome


2. Using replaceAll() method


We can also use the replaceAll() method to replace space with an underscore in Java. The syntax of the  replaceAll() method is identical to replace() method, but it takes regex as an argument as shown below:

Syntax of replaceAll() method


public String replaceAll(String regex,
                         String replacement)


According to Oracle docs, the replaceAll() method replaces each substring of the given string that matches the given regular expression with the given replacement.

public class ReplaceSpaceWithUnderscore2 {
    public static void main(String args[]) {
        String str = "Be in Present";
        str = str.replaceAll(" ","_");
        System.out.println(str);
    }
}


Output:
Be_in_present


Sometimes, you want to replace more than one(consecutive) whitespaces in the given string with an  underscore. You can use regex "\\s+" in the replaceAll() method to achieve the desired result as shown below in the example.

public class ReplaceSpaceWithUnderscore3 {
    public static void main(String args[]) {
        String str = "Be    in     Present";
        str = str.replaceAll("\\s+ ","_");
        System.out.println(str);
    }
}


Output:
Be_in_present


If you do not want spaces before the start and after the end of the given string in the output, then call the  trim() method before calling the replaceAll() method as shown below in the example:

public class ReplaceSpaceWithUnderscore4  {
    public static void main(String args[]) {
        String str = "    Be    in     Present      ";
        str = str.trim().replaceAll("\\s+ ","_");
        System.out.println(str);
    }
}


Output:
Be_in_present


That's all for today. Please mention in the comments if you have any questions related to how to replace space with an underscore in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry