How to replace multiple consecutive question marks (?) with a single question mark in Java

1 Answer

0 votes
public class ReplaceQuestionMarks {
    public static void main(String[] args) {
        String str = "Hello??? How are you?? What is your Wi-Fi password????";

        // Replace multiple '?' with a single '?'
        String result = str.replaceAll("\\?+", "?");

        System.out.println(result);
    }
}



/*
run:

Hello? How are you? What is your Wi-Fi password?

*/

 



answered Jul 24, 2025 by avibootz
...