Replacing a character in a string Java
In this post, you will learn how to replace a character in a string using the Java programming language. Such a type of logical coding question is generally asked in a programming interview or competitive examination. We should be aware that the solution to the most fundamental coding or programming questions decides how we perform in an interview. There are several ways to replace a character in a given string. Here, we have mentioned most of them.
Java replace a character in a string using replace() method
The replace() method of Java searches a string for a specified character, and returns a new string where the specified character(s) are replaced.
public String replace(char searchChar, char newChar)
It returns a new string, where the specified character has been replaced by the new character(s).
public class Main {
public static void main(String[] args) {
String str = "Welcame";
System.out.println("Original string: "+str);
System.out.println("Modified string: "+str.replace('a', 'o'));
}
}
Output of the above code:
Original string: Welcame
Modified string: Welcome
Replacing a character in a string using the String class
In Java, string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object. Here, we construct a new String with two different substrings, one from the beginning till the specific (index-1), the new character at the specific index, and the other from the (index+1) till the end.
public class ReplaceChar {
public static void main(String args[])
{
// initializing string
String str = "Welcome fo school";
// defining the index
int index = 8;
// initializing the character
char ch = 't';
// Printing the original string
System.out.println("Original string: " + str);
str = str.substring(0, index) + ch
+ str.substring(index + 1);
// Printing the modified string
System.out.println("Modified string: " + str);
}
}
Output of the above code:
Original string: Welcome fo school
Modified string: Welcome to school
Java replace a character in a string using StringBuilder
StringBuilder in Java represents a mutable sequence of characters that can be modified with characters. The StringBuilder class provides high performance compared with other string classes. It provides other methods for various purposes. In the given Java program, we replace the character at a specific index using the method setCharAt().
public class ReplaceChar {
public static void main(String args[])
{
// Initializing the string
String str = "Came back home";
// Initializing the index
int index = 1;
// Getting the character
char ch = 'o';
// Printing the original string
System.out.println("Original String: " + str);
StringBuilder string = new StringBuilder(str);
string.setCharAt(index, ch);
// Printing the modified string
System.out.println("Modified String: " + string);
}
}
Output of the above code:
Original String: Came back home
Modified String: Come back home
Java replace a character in a string using StringBuffer
The java.lang.StringBuffer class is a thread-safe, mutable sequence of characters. This class provides a predefined method setCharAt() that replace the character at the specific index.
public void setCharAt(int index, char ch)
In this method, the character at the specified index is set to ch.
public class ReplaceChar {
public static void main(String args[])
{
// Initializing the string
String str = "Welcame back";
// Initializing the index
int index = 4;
// Initializing the character
char ch = 'o';
// Printing the original string
System.out.println("Original String: " + str);
StringBuffer string = new StringBuffer(str);
string.setCharAt(index, ch);
// Printing the modified string
System.out.println("Modified String: " + string);
}
}
Output of the above code:
Original String: Welcame back
Modified String: Welcome back
Related Articles
Enum from string JavaJava enum
Sort array in ascending order Java
String reverse in Java
Count vowels in a string Java
Java compare two strings
Java string split multiple delimiters
Char array to string Java
Java find largest of three numbers
Vowel and Consonant program in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java