JAVA program for string method replace()

JAVA program for string method replace()

This JAVA program is for string method replace().

String method replace(char, char) replaces all occurrences of the first character with the second character passed to the method.

For example  if string str = ‘cidedist’, then str.replace(‘i’, ‘o’) will give string ‘codedost’.

Dry Run of the Program

Take input ‘str’.Let us take input str = ‘CIDEDIST’

str.replace(‘I’, ‘O’) = ‘CODEDOST‘ (replaces all characters in string with letter I to letter O)

Program

import java.util.*;

class replace
{
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the string");
		String str = sc.nextLine();
		
		System.out.println("On using .replace() = "+str.replace('I', 'O'));
	}
}

Output

Share Me!