Export (0) Print
Expand All
Expand Minimize

java.lang.String.replace(char oldChar, char newChar)

Visual Studio 2005

Replaces all existing occurrences of a character in a String with another character.


public String replace(char oldChar, char newChar)

Parameters

Parameter Description

oldChar

The existing character to replace.

newChar

The character to replace oldChar with.

A new String object where all existing occurrences of oldChar have been replaced with newChar.

// string_replace.jsl

public class Program
{
    public static void main(String[] args)
    {
        String str = "The quick brown fox jumps over the lazy dog.";

        // Replace all the 'd' characters with 'l' characters.
        String newStr = str.replace('d', 'l');

        // Display the strings for comparison.
        System.out.println("old = " + str);
        System.out.println("new = " + newStr);
    }
}

/*
Output:
old = The quick brown fox jumps over the lazy dog.
new = The quick brown fox jumps over the lazy log.
*/

Community Additions

ADD
Show:
© 2014 Microsoft