Java - Remove All Vowels from a String in Java.


IncludeHelp 07 August 2016

In this code snippet, we will learn how to remove all vowels from a string in Java, to implement this logic we are using a loop from 0 to length-1 (here length is the string length) and checking for vowels.

Except of vowel character, other all characters are assigning into another string and that anther string will be the final string that will contain text without any vowels.

We created a method isVowel(), this method will return true if character is vowel else it will return false.

Java Code Snippet - Remove All Vowels from a String in Java

//Java - Remove Vowels from a String in Java.

public class RemoveVowels
{
	 //function to check character is vowel or not
	  public static boolean isVowel(char ch){
		  switch(ch){
		  	case 'A': case 'E': case 'I': case 'O': case 'U':
		  	case 'a': case 'e': case 'i': case 'o': case 'u':
		  		return true;
		  	default:
		  		return false;		  	
		  }
	  }
	  
	  public static void main(String args[]){
		  String text="Hello World!";
		  String text1="";
		  char ch;
		  
		  System.out.println("String before removing vowels: " + text);
		  
		  for(int loop=0; loop<text.length(); loop++){
			  ch=text.charAt(loop);
			  if(!isVowel(ch)){
				  text1+=ch;
			  }
		  }			  
		  System.out.println("String after removing vowels: " + text1);
	  }
}
    

    String before removing vowels: Hello World!
    String after removing vowels: Hll Wrld!



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.