Vidyalelo
Java Programming · Q12

Strings

Programming · Java Programming · question 12

Q12

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

A.
replace()
Answer
B.
replaceAll()
C.
replaceChar()
D.
replaceAllOccurrences()

Answer: Option A

Solution

Answer: Option A
Solution:
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.