sayhaan
sayhaan

Reputation: 5

Removing vowels only if they do not begin a word

I am trying to remove the vowels from a string longer than 10 characters, except if they begin the string or if they begin a word. I can't figure out how to only remove those specific vowels.

I've tried using string.replaceAll, and various combination of spacing and trying to separate the vowels. I'm extremely new to java, and can't figure out what else to do. The only other thread I've found perfectly describing my problem has been for python.

String x = scan.nextLine();
    if(x.length() >= 10){
        x.toLowerCase();
        String y = x.replaceAll("[aeiou]", "");
        System.out.println(y);
    }else{    
        System.out.println("This doesn't need shortening!");

If the user inputs "The quick brown fox jumps over the extremely lazy dog", the output should be "Th qck brwn fx jmps ovr th extrmly lzy dg"

Upvotes: 0

Views: 101

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521053

I would use the pattern (?<=\S)[aeiou] in case insensitive mode:

String input = "Hello how are you?";
System.out.println(input);
input = input.replaceAll("(?i)(?<=\\S)[aeiou]", "");
System.out.println(input);

This prints:

Hello how are you?
Hll hw ar y?

The positive lookbehind (?<=\S) asserts that what precedes each matching vowel is not whitespace.

Upvotes: 2

Related Questions