Skip to content

Instantly share code, notes, and snippets.

@AnEmortalKid
Created September 10, 2015 15:52
Show Gist options
  • Save AnEmortalKid/015e86ea4a49a30dcc79 to your computer and use it in GitHub Desktop.
Save AnEmortalKid/015e86ea4a49a30dcc79 to your computer and use it in GitHub Desktop.
public class StringReplaceStuff {
public static void main(String[] args) {
String testString = "Hello World!";
// replace o with 0, replaceAll takes a string pattern to replace
String osReplaced = testString.replaceAll("o", "0");
System.out.println("Without O = " + osReplaced);
// if we want to add to our replacement, we need to do it in different
// step strings
String esReplaced = osReplaced.replaceAll("e", "3");
System.out.println("Without O and E= " + esReplaced);
// otherwise if we just want to print different transformations
System.out.println(testString.replaceAll("o", "0"));
System.out.println(testString.replaceAll("e", "3"));
/*
* Note: replaceAll returns a MODIFIED string, Strings are immutable so
* you need to keep assigning the result of replaceAll in order to
* achieve your transformation
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment