0

How to replace a ? from string with another. Here is my sample code

String oldString = "I am ? ?";
String newString = oldString.replaceAll("?|?", "Java|Developer");
System.out.println(newString);

and gives error

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0

If I use another way like

String oldString = "I am J D";
String newString = oldString.replaceAll("J|D", "Java|Developer");
System.out.println(newString);

and the output is

I am Java|Developer Java|Developer

My requirement is

Input: I am ? ?
Output: I am Java Developer

Is there any way to achieve it?

0 Answers0