Skip to content

Instantly share code, notes, and snippets.

@reza-ryte-club
Created August 22, 2015 07:50
Show Gist options
  • Save reza-ryte-club/9c0288c8a47d631084a3 to your computer and use it in GitHub Desktop.
Save reza-ryte-club/9c0288c8a47d631084a3 to your computer and use it in GitHub Desktop.
Replace a character with a string
public class ReplaceCharacter {
public static String stringReplacer(String str,char source,String replaced){
StringBuilder dest = new StringBuilder();
for(int i = 0;i<str.length();i++){
char ch = str.charAt(i);
if(ch==source){
dest.append(replaced);
}
else{
dest.append(ch);
}
}
return dest.toString();
}
public static void main(String[] args) {
System.out.println("reverse string");
String word = "Hello world";
String word2 = "Hello wordl";
System.out.println(stringReplacer(word,' ', word2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment