Search code examples
javareplaceall

How to replace vowels with another character


How can I replace all the vowels in s1 with z

public class q3 {
    public static void main(String[] args) {

        String s1="Hello World";
        System.out.println(s1.replace("a,e,i,o,u","z"));


    }
}

Solution

  • System.out.println(s1.replaceAll("[aeiou]", "z"));
    

    If you want to make it case insensitive then use

    System.out.println(s1.replaceAll( "(?i)[aeiou]", "z" ));