Latest web development tutorials

Java replace () method

Java String class Java String class


replace () method replaces all oldChar character string with newChar character, and returns the new replacement string.

grammar

public String replace(char oldChar,
                      char newChar)

parameter

  • oldChar - the original characters.

  • newChar - the new character.

return value

Replaced with a new string after generation.

Examples

public class Test {
	public static void main(String args[]) {
		String Str = new String("hello");

		System.out.print("返回值 :" );
		System.out.println(Str.replace('o', 'T'));

		System.out.print("返回值 :" );
		System.out.println(Str.replace('l', 'D'));
	}
}

The above program execution results:

返回值 :hellT
返回值 :heDDo

Java String class Java String class