Created
August 22, 2015 07:50
-
-
Save reza-ryte-club/9c0288c8a47d631084a3 to your computer and use it in GitHub Desktop.
Replace a character with a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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