1

The code bellow removes the vowels form the string but it does not remove the spaces that corresponds before or after the "_".

public static String removeVowels(String str) {
    str = str.replaceAll("[aeiouAEIOU]",""); 


    return str;

}

public static void main(String[] args) {
    System.out.println("__" + removeVowels(" The Lion king ") + "__");

}
}

Console Output:

__ Th Ln kng __

What the console output should be:

__Th Ln kng__

Not really sure how I would go about doing this without going into the main method and manually removing the spaces.

D. Lee
  • 21
  • 3
  • You're question title is "removing spaces from a string" -- but it sounds like you want a much more specific case - of removing spaces after the first two `_` and before the last two `_`?. From your code it looks like you've created the bug in your test cases: `" Snowboarding school "` has a leading and a trailing space... – Don Cheadle May 10 '16 at 19:26

2 Answers2

4

Not sure what you're asking, but perhaps you want to trim() your String. This removes white space from the front and back ends of the String:

str = str.replaceAll("[aeiouAEIOU]","").trim(); 
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Use:

str = str.replaceAll("\s*__\s*",""); 
Jens
  • 67,715
  • 15
  • 98
  • 113