Replace Spaces of String with a Character in Java

In this tutorial we will learn writing java program to replace blank spaces of a String with a given character. The program will accept one string and one character from the user.

  1. The main string where the space is needed to be replaced.
  2. The character which replaces the space in 1st string.
  3. And then returns the final string where the space of main string is replaced by character inserted by user.

For example

Case 1:  If user is given the string value as ‘ques ol’,

                 and the character value inserted by user is ‘c’

                 then the output generated must be “quescol”.

Case 2:  If user is given the string value as ‘java sfun’,

                 and the character value inserted by user is ‘i’

                 then the output generated must be “javaisfun”.

Program 1 : Replace Space with Given Input using replace() method Java

import java.util.*;
public class Main
{
  public static void main (String[]args)
  {
    System.out.println("Java Program to Replace the spaces with given Character");
    Scanner sc = new Scanner (System.in);
    System.out.println ("Please enter a String");
    String str = sc.nextLine();
    System.out.println ("Please enter a Character to replace space");
    char ch = sc.next ().charAt (0);
    str = str.replace(' ', ch);
    System.out.println (str);
  }
}

Output

Java Program to Replace the spaces with given Character
Please enter a String
quescol Website
Please enter a Character to replace space
p
quescolpWebsite

Explanation:

Here, the user input string ‘quescol Website’, and a character ‘p’ which replaces all the spaces in string. In order to search all the spaces on sting and replaces it with character, replace function helps to perform this operation. And returns the copy of string ‘quescol Website’ where all spaces if replaced with ‘p’ as we can see in the output generated.

Program 2: Replace Space with Given Input without using replace() method Java

import java.util.*;
public class Main
{
  public static void main (String[]args)
    {
        System.out.println("Java program to replace the spaces of a string");
        Scanner sc = new Scanner (System.in);
        System.out.println ("Please enter a String");
        String str = sc.nextLine();
        StringBuffer newStr=new StringBuffer(str); 
        System.out.println("Please enter a Character to replace space");
        char ch = sc.next().charAt(0); 
        for(int i = 0; i < str.length(); i++) {      
            if(str.charAt(i) == ' ') {    
                newStr.setCharAt(i, ch);    
            }   
        }    
        System.out.println(newStr);
    }
}

Output

Java program to replace the spaces of a string
Please enter a String
I am quescol website
Please enter a Character to replace space
t
Itamtquescoltwebsite

Explanation

Here, the user input string ‘I am quescol website’, and a character ‘t’ which replaces all the spaces in string. In order to search all the spaces on string and replaces it with character as we can see in the output generated.

Program 3: Replace space with given Character in string Java

import java.util.*;
public class Main
{
  public static void main (String[]args)
    {
        System.out.println("Java program to replace the spaces of a string");
        Scanner sc = new Scanner (System.in);
        System.out.println ("Please enter a String");
        String inputString = sc.nextLine();
        System.out.println ("Please enter a Character to replace space");
        char ch = sc.next().charAt(0);  
        String newString = inputString.chars()
                .mapToObj(c -> (char) c == ' ' ? ch : (char) c)
                .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
                .toString();
        System.out.println(newString);
    }
}

Output

Java program to replace the spaces of a string
Please enter a String
I am quescol website
Please enter a Character to replace space
u
Iuamuquescoluwebsite

Conclusion

In this tutorial we have explained how to write a Java program to replace all the spaces in a given string with a specified character. The program takes string from user as input and also a character to replace spaces. Then we have applied our logics to replace all the spaces with the given character.

In the first program we have used inbuilt String replace() method. This process was easy straightforward and concise. The user input string undergoes space replacement using the specified character, resulting in the final modified string.

In the second program we have applied our own logic instead of using replace() method. We have used for loop to iterate through the characters of the input string, replacing spaces with the specified character. This program showcases an alternative approach to achieve the desired outcome.

And in third program we have used Java 8 streams to efficiently replace spaces in the input string with the given character. It demonstrates the power of streams and functional programming concepts to achieve the desired string transformation.

In summary, these article provides multiple ways to replace spaces in a string with a specified character. I hope this tutorial helped you to understand the program of replace blank spaces of character string java.