Replace Space With %20 in Java

Shubham Vora Jan 30, 2023 Nov 15, 2022
  1. Use the replaceAll() Method to Replace Space With %20 in Java
  2. Use the StringBuilder Class and Create a New String to Replace Space With %20 in Java
Replace Space With %20 in Java

In this article, we will learn two methods to replace all spaces of the given string with %20.

Use the replaceAll() Method to Replace Space With %20 in Java

Here, we use the replaceAll(), a built-in Java method, to replace all spaces with a %20 string.

In the example, we’ve created the string and executed the replaceAll() method by taking that string as a reference.

Syntax of the replaceAll() method:

string.replaceAll(oldString,newString);

As users can see in the syntax of the replaceAll() method, it takes the string as the first parameter that needs to be replaced and a new string as a second parameter that will replace the first.

Example Code:

class Test {
    public static void main(String[] args) {
         // given string
        String str = "Hello user!, Welcome to the DelftStack.   ";
        // replace spaces with %20
        str = str.replaceAll(" ", "%20");
        System.out.println(str);
    }
}

Output:

Hello%20user!,%20Welcome%20to%20the%20DelftStack.%20%20%20

In the above output, users can see that replaceAll() has replaced all spaces with %20, even all spaces at the end of the string. If users want to remove the spaces from the end, they should use the trim() method of the String library.

str = str.trim().replaceAll(" ", "%20");

Output:

Hello%20user!,%20Welcome%20to%20the%20DelftStack.

Now, users can observe in the above output that the trim() method has removed the spaces from the end, and that’s why the replaceAll() method hasn’t added the %20 string at the end.

Time Complexity

The time complexity of the above example is O(n), where n represents the string length. The time complexity of the replaceAll() method is O(n).

Space Complexity

The space complexity of the code shown above is O(1), as we are not using any extra spaces.

Use the StringBuilder Class and Create a New String to Replace Space With %20 in Java

In this method, we will use the StringBuilder() class to create a string of the custom length. Users should follow the steps below to replace all spaces with the %20 string by using the extra space.

  • Create an object of the StringBuilder class and initialize it with an empty string. Here, we have created the resultString.
  • Iterate through the given string using for loop or a while loop.
  • If the character at position i is space in the given string, append %20 to resultString. Otherwise, append the same character to resultString.
  • Return or print the resultString once the iteration is completed.

Example Code:

class Test {
    public static void main(String[] args) {
        StringBuilder resultString = new StringBuilder("");
        // given string
        String str = "Java is One of the best Programming Language.";
        // Remove spaces from the end of the string.
        str = str.trim();
        // iterate through string
        for(int i=0;i<str.length();i++){
        // if the character at position i is a string, append %20 to string builder. Otherwise, append same character,
            if(str.charAt(i)==' '){
                resultString.append("%20");
            }else{
                resultString.append(str.charAt(i));
            }
        }
        // print the StringBuilder after appending
        System.out.println(resultString);
    }
}

Output:

Java%20is%20One%20of%20the%20best%20Programming%20Language.

Time Complexity

The above algorithm has O(n) time complexity, where n refers to the length of the given string as we are iterating through the string.

Space Complexity

The space complexity of the above algorithm is O(n), as we use the StringBuilder() class to create and store the new string.

We have learned two methods to replace a substring or character in a given string with a new substring. In the first part, we used the built-in replaceAll() method of Java and created the custom algorithm in the second part.

It is recommended to use the replaceAll() method as it is easy to use and a single line of code.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub