How to replace a substring inside a string by another one ?


Levels of difficulty: / perform operation:

JAVA Program

public class StringReplaceEmp {
	public static void main(String args[]) {
		String str="Hello World";
		System.out.println( str.replace( 'H','W' ) );
		System.out.println( str.replaceFirst("He", "Wa") );
		System.out.println( str.replaceAll("He", "Ha") );
	}
}

Output

Wello World
Wallo World
Hallo World