String Class Tutorials

String replace(char oldChar, char newChar)
String replace(CharSequence target, CharSequence replacement)

The replace() method is overloaded and has two different signatures. The replace(char oldChar, char newChar) simply returns a string with all characters that match the first parameter oldchar replaced with the character specified by the second parameter newChar. For example:
String s = "Don't bring these to the airport: pocket knife; liquid bleach; gasoline; guns; etc.";
String version1 = s.replace(';', ',');
System.out.println( version1);
// Don't bring these to the airport: pocket knife, liquid bleach, gasoline, guns, etc.

The replace(CharSequence target, CharSequence replacement) simply returns a string with all substrings that match first parameter target replaced with the substring specified by the second parameter replacement. For example:
String u = "The quick brown fox jumps over the lazy dog.";
u = u.replace("the", "my");
System.out.println(u);
// The quick brown fox jumps over my lazy dog.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md StringReplace
C:\Java>cd StringReplace
C:\Java\StringReplace>Notepad StringReplace.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class StringReplace {
    public static void main(String args[]) {
        String s = "Don't bring these to the airport: pocket knife; liquid bleach; gasoline; guns; etc.";
        String version1 = s.replace(';', ',');
        System.out.println(version1); // version1 = Don't bring the following items to the airport: pocket knife, liquid bleach, gasoline, guns, etc.

        String t = "WYZ WYZ WYZ";
        t.replace('W', 'X'); // common mistake - return value goes nowhere
        System.out.println(t);
        t = t.replace('W', 'X'); // fixed
        System.out.println(t);

        String u = "The quick brown fox jumps over the lazy dog.";
        u.replace("the", "my"); // common mistake - return value goes nowhere
        System.out.println(u);
        u = u.replace("the", "my"); // fixed
        System.out.println(u);
    }	
}


Now switch back to the command prompt (CMD) and type in javac StringReplace.java and press Enter.
Now type in java StringReplace and press Enter.


C:\Java\StringReplace>javac StringReplace.java
C:\Java\StringReplace>java StringReplace
Don't bring these to the airport: pocket knife, liquid bleach, gasoline, guns, etc.
WYZ WYZ WYZ
XYZ XYZ XYZ
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over my lazy dog.


Final thoughts

One strategy that I regularly use to solidify my knowledge of Java is to browse through the actual source code. Due to some language in their copyright file, I can't actually show the source code in my videos. The source code is contained inside of a file called src.zip located in the folder where your JDK is installed. I recommend using notepad++ to browse the class files.


Tutorials