How do you replace a specific character in a string in Java?
In Java, you can use the replace() method of the String class to replace a character in a string. This method takes two parameters, the first one is the character to be replaced and the second one is the character to replace it with. Here is an example code:
String str = "Hello World";
String replacedStr = str.replace('o', 'x');
System.out.println(replacedStr);
The code above will replace all ‘o’ characters in the string with ‘x’ characters, and the output will be “Hellx Wxrld”. Please note that the replace() method returns a new string and does not modify the original string.