replace chacter in string java code example

Example 1: 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

Example 2: string replace java

public static void main(String args[]){  
String s1="my name is khan my name is java";  
String replaceString=s1.replace("is","was");//replaces all occurrences of "is" to "was"  
System.out.println(replaceString);  
}}