Examveda
Examveda

Which of the following methods is used to replace all occurrences of a specified character in a string in Java?

A. replace()

B. replaceAll()

C. replaceChar()

D. replaceAllOccurrences()

Answer: Option A

Solution(By Examveda Team)

replace() is a method in Java used to replace all occurrences of a specified character or substring in a string with another character or substring.

It takes two parameters: the character or substring to be replaced and the new character or substring.

replaceAll() is used for replacing all substrings that match a regular expression, which is different from replace().

replaceChar() and replaceAllOccurrences() are not valid methods in Java.

This Question Belongs to Java Program >> Strings

Join The Discussion

Comments ( 1 )

  1. Ravindra Singh
    Ravindra Singh :
    5 months ago

    Correct answer for the given question should be replace() .

Related Questions on Strings