String Modifying Methods with Examples

String modification is an essential task in programming, and Java provides several methods to perform such operations. While strings in Java are immutable, these methods return new strings with the modifications applied. In this article, we will see some common string-modifying methods with examples.

    String Modifying Methods with Examples

    1. concat(String str) 

    Concatenates the specified string to the end of this string.
    String str = "Java";
    String result = str.concat("Guides");
    // Result: "JavaGuides"

    2. replace(char oldChar, char newChar) 

    Replaces all occurrences of a specified character with another character.
    String str = "Java";
    String result = str.replace('a', 'o');
    // Result: "Jovo"

    3. substring(int beginIndex) and substring(int beginIndex, int endIndex) 

    Returns a substring from the original string.
    String str = "JavaGuides";
    String result1 = str.substring(4);
    // Result: "Guides"
    
    String result2 = str.substring(4, 6);
    // Result: "Gu"

    4. toLowerCase() and toUpperCase() 

    Converts the string to lower case or upper case.
    String str = "Java";
    String lower = str.toLowerCase();
    // Result: "java"
    
    String upper = str.toUpperCase();
    // Result: "JAVA"

    5. trim() 

    Removes leading and trailing white spaces.
    String str = " JavaGuides ";
    String result = str.trim();
    // Result: "JavaGuides"

    6. split(String regex) 

    Splits the string around matches of the given regular expression.
    String str = "Java,Guides";
    String[] result = str.split(",");
    // Result: ["Java", "Guides"]

    7. replaceAll(String regex, String replacement) 

    Replaces each substring that matches the given regular expression with the given replacement.
    String str = "JavaGuides";
    String result = str.replaceAll("a", "o");
    // Result: "JovoGuides"

    8. join(CharSequence delimiter, CharSequence... elements) 

    Joins the given elements with the specified delimiter.
    String result = String.join("-", "Java", "Guides");
    // Result: "Java-Guides"

    Summary

    Method Description
    concat(String str) Concatenates the specified string to the end of this string.
    replace(char oldChar, char newChar) Replaces all occurrences of a specified character with another character.
    substring(int beginIndex) Returns a substring from the specified index to the end.
    substring(int beginIndex, int endIndex) Returns a substring from the specified beginning index to the end index.
    toLowerCase() Converts all characters in the string to lowercase.
    toUpperCase() Converts all characters in the string to upper case.
    trim() Removes leading and trailing white spaces.
    split(String regex) Splits the string around matches of the given regular expression.
    replaceAll(String regex, String replacement) Replaces each substring that matches the given regular expression with the given replacement.
    join(CharSequence delimiter, CharSequence... elements) Joins the given elements with the specified delimiter.

    Comments