Signup/Sign In

Java Program to Replace the Spaces of a String with a Specific Character

In this tutorial, we will learn how to replace the spaces of a string with a specific character. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the string: A bird in the hand is worth two in the bush

Enter the character: -

Output: After replacing the spaces of a string with a specific character:

A-bird-in-the-hand-is-worth-two-in-the-bush

Program 1: Replace the Spaces of a String with a Specific Character

In this program, we will see how to replace the spaces of a string with a specific character when the values are pre-defined in the program. Here, we will use the replace() method to replace the white space with the given specified character.

Algorithm

  1. Start
  2. Declare a string.
  3. Initialize it.
  4. Print the entered string before replacing the spaces with the specified character.
  5. Define the character with which the spaces are to be replaced.
  6. Use the replace() method to replace the spaces with the given specified character.
  7. Now, print the entered string after replacing the spaces with the specified character.
  8. Stop.

Below is the code for the same in Java language.

//Java Program to replace the spaces of a string with a specific character
public class Main   
{    
    public static void main(String[] args) 
    {    
        String string = "Actions speak louder than words";    
        char ch = '-';            
        System.out.println("String before replacing spaces with given character: ");    
        System.out.println(string);         
        //Replace space with specific character ch    
        string = string.replace(' ', ch);                
        System.out.println("String after replacing spaces with given character: ");    
        System.out.println(string);    
    }    
}      


String before replacing spaces with given character:
Actions speak louder than words
String after replacing spaces with given character:
Actions-speak-louder-than-words

Program 2: Replace the Spaces of a String with a Specific Character

In this program, we will see how to replace the spaces of a string with a specific character when the values are pre-defined in the program. Here, we will use the replaceAll() method to replace all the white space with the given specified character.

Algorithm

  1. Start
  2. Declare a string.
  3. Initialize it.
  4. Print the entered string before replacing the spaces with the specified character.
  5. Define the character with which the spaces are to be replaced.
  6. Use the replaceAll() method to replace the spaces with the given specified character.
  7. Now, print the entered string after replacing the spaces with the specified character.
  8. Stop.

Below is the code for the same in Java language.

//Java Program to replace the spaces of a string with a specific character
public class Main   
{    
    public static void main(String[] args) 
    {    
        String string = "Slow and steady wins the race";    
        char ch = '-';           
        System.out.println("String before replacing spaces with given character: ");    
        System.out.println(string);         
        //Replace space with specific character ch    
        string = string.replaceAll(" ", "-");               
        System.out.println("String after replacing spaces with given character: ");    
        System.out.println(string);    
    }    
}      


String before replacing spaces with given character:
Slow and steady wins the race
String after replacing spaces with given character:
Slow-and-steady-wins-the-race

Program 3: Replace the Spaces of a String with a Specific Character

In this program, we will see how to replace the spaces of a string with a specific character when the values are pre-defined in the program. Here, we will use the for and if loop to replace the white space with the given specified character.

Algorithm

  1. Start
  2. Declare a string.
  3. Initialize it.
  4. Print the entered string before replacing the spaces with the specified character.
  5. Define the character with which the spaces are to be replaced.
  6. Use a for and if loop for the same.
  7. Use the for loop to iterate over each character of the string.
  8. Use the if loop to check if any space is present or not.
  9. If any space encounters, then replace it with the specified character.
  10. Now, print the entered string after replacing the spaces with the specified character.
  11. Stop.

Below is the code for the same in Java language.

//Java Program to replace the spaces of a string with a specific character
public class Main
{ 
    // Function to replace Space with - 
    static String replaceStr(String str) 
    {           
        String s = "";           
        // Iterate over each character of the string
        for (int i = 0; i < str.length(); ++i) 
        {       
            // If a space encounters then replace it with -
            if (str.charAt(i) == ' ')  
                s += '-';               
            else
                s += str.charAt(i);               
        } 
        // return the new string. 
        return s; 
    }       
    //Driver Code  
    public static void main(String []args) 
    { 
        // Initialize the String 
        String str = "There are other fish in the sea"; 
        System.out.println("String before replacing spaces with given character: ");    
        System.out.println(str);    
        //Print the modified string
        System.out.println("String after replacing spaces with given character: ");    
        System.out.println(replaceStr(str));     
    } 
} 


String before replacing spaces with given character:
There are other fish in the sea
String after replacing spaces with given character:
There-are-other-fish-in-the-sea



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.