how to change single character of a string in java
//short answer: you cannot individually change any specific character
//of a String in java. You can however do this:
String s1 = "This is a String";
String s2 = s1.substring(0, 8) + "o" + s1.substring(9);
System.out.println(s2);
//Prints "This is o String", replaced the 8th character with an o
// Algorithm to replace a specific character from end of string.
// From: 10.000.000.00
// To: 10.000.000,00
String cash = "10.000.000.00";
StringBuilder temp1 = new StringBuilder(cash);
StringBuilder temp2 = new StringBuilder(temp1.reverse().toString().replaceFirst("[.]", ","));
System.out.println(temp2.reverse());
// You cannot change the characters of a string in java
// But you can make it work
// For Example: A Program to change 'r' to 'a'
class Main{
public static void main(String args[]){
String str = "return";
String res = "";
for(int i = 0; i<str.length(); i++){
if(str.charAt(i)=='r'){
res+='a';
}
else{
res+=str.charAt(i);
}
}
System.out.print("Result = "+res);
}
}
how to replace a character with another character in a string in java
public class JavaExample{
public static void main(String args[]){
String str = new String("Site is BeginnersBook.com");
System.out.print("String after replacing com with net :" );
System.out.println(str.replaceFirst("com", "net"));
System.out.print("String after replacing Site name:" );
System.out.println(str.replaceFirst("Beginners(.*)", "XYZ.com"));
}
}