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
- Start
- Declare a string.
- Initialize it.
- Print the entered string before replacing the spaces with the specified character.
- Define the character with which the spaces are to be replaced.
- Use the replace() method to replace the spaces with the given specified character.
- Now, print the entered string after replacing the spaces with the specified character.
- 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
- Start
- Declare a string.
- Initialize it.
- Print the entered string before replacing the spaces with the specified character.
- Define the character with which the spaces are to be replaced.
- Use the replaceAll() method to replace the spaces with the given specified character.
- Now, print the entered string after replacing the spaces with the specified character.
- 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
- Start
- Declare a string.
- Initialize it.
- Print the entered string before replacing the spaces with the specified character.
- Define the character with which the spaces are to be replaced.
- Use a for and if loop for the same.
- Use the for loop to iterate over each character of the string.
- Use the if loop to check if any space is present or not.
- If any space encounters, then replace it with the specified character.
- Now, print the entered string after replacing the spaces with the specified character.
- 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