Java Strings: Replace Characters
The replace() Method
The replace() method takes two arguments, chars or Strings, and replaced every instance of the first argument with the second argument. The general syntax is either:
string.replace(‘char1’, ‘char2’);
string.replace(“string1”, “string2”);
Note how we contain these chars and Strings, the same old rules apply:
- chars are contained within single quotes: ‘char’
- Strings are contained within double quotes: “String”
Let’s see this methods in action by replacing a string within a string and a characters within a string:
String stringSchool="Learning Glue is an awesome place to learn"; System.out.println("Original Name string: " + stringSchool); //Replace all instances of 'a' with 'e' String stringSchoolReplaced=stringSchool.replace('a','e'); System.out.println("Replaced Name string: " + stringSchoolReplaced); String stringTagLine="Making IT Stick"; System.out.println("Original Tag Line string: " + stringTagLine); //Replace all instances of "IT" to "KNOWLEDGE" String stringTagLineReplaced=stringTagLine.replace("IT","KNOWLEDGE"); System.out.println("Replaced Tag Line string: " + stringTagLineReplaced);
The output returns both the original and modified strings:
run: Original Name string: Learning Glue is an awesome place to learn Replaced Name string: Leerning Glue is en ewesome plece to leern Original Tag Line string: Making IT Stick Replaced Tag Line string: Making KNOWLEDGE Stick BUILD SUCCESSFUL (total time: 0 seconds)
Advanced replace methods
The following replace methods are included here for completeness. Regular Expressions are an optional component of these methods are are beyond the scope of this course.
The replaceAll() Method
Similar in outcome to the replace() method, the replaceAll() method will replace each substring that matches a given regular expression.
Regular Expressions are special sequences of characters that Java can use to identify characters and strings. It does this by using a special syntax contained within a pattern. Regular Expressions are frequently used to search for and edit text and data. Regular Expressions are beyond the scope of this course but will be included in an advanced Java course.
The general syntax is:
String.replaceAll(String regex, String replacement);
This method will also work without a Regular Expression. Here, we will use the replaceAll() method to replace only all instances of the substring “Welcome” in a string that contains three instances of that substring:
public static void main(String[] args){ String Str = new String("Welcome, Welcome to learningglue.com, Welcome!"); System.out.println("Source Value: " + Str ); System.out.print("Return Value: " ); System.out.println(Str.replaceAll("Welcome", "")); }
The output shows the string with all instance of the substring “welcome” missing:
run: Source Value: Welcome, Welcome to learningglue.com, Welcome! Return Value: , to learningglue.com, ! BUILD SUCCESSFUL (total time: 0 seconds)
The replaceFirst() Method
This method replaces the first substring of a string that matches a given regular expression.
The general syntax is:
String.replaceFirst(String regex, String replacement);
This method will also work without a Regular Expression. Here, we will use the replaceFirst() method to replace only the first instance of the substring “Welcome” in a string that contains three instances of that substring:
public static void main(String[] args){ String Str = new String("Welcome, Welcome to learningglue.com, Welcome!"); System.out.print("Return Value: " ); System.out.println(Str.replaceFirst("Welcome ", "")); }
The output shows the string with only the first “welcome” substring missing:
run: Return Value: , Welcome to learningglue.com, Welcome! BUILD SUCCESSFUL (total time: 0 seconds)