# (java) I need to remove all vowels from a string



## -fool- (Dec 21, 2005)

Im trying to make a method that removes all the vowels from a string.. and i dont really know whats the way to do it. im thinking use booleans for each one, and

```
public void vowelRemover(string b)
{
     while(a == true)
     {
          int aspot = b.indexOf("a");
          b = b.delete(a, a,);
```
but then i dont know hwo to make it stop . this is pretty hard.


----------



## Chicon (Jul 29, 2004)

-fool- said:


> Im trying to make a method that removes all the vowels from a string.. and i dont really know whats the way to do it. im thinking use booleans for each one, and
> 
> ```
> public void vowelRemover(string b)
> ...


hi -fool- ,

Welcome to TSG ! 

I give you a tip :


String test = "whatever you want";
test = test.replaceAll("a","");
test = test.replaceAll("e","");
... and so on. 

Also, don't forget to remove lowercase and uppercase vowels.

Example :


```
[FONT="Courier New"][SIZE="4"]
public class MyClass {
  
// attributes
   private String VOWELS = "aeiouyAEIOUY";

// constructor  
     public MyClass() {
     }
  
// the vowels removal method
    public String vowelRemove(String test) {
       int len = VOWELS.length();
       String vowel;
       if (test.length() > 0) {
           for (int i = 0; i < len; i++) {
               vowel = VOWELS.substring(i, 1);
               test = test.replaceAll(vowel, "");
           }
       }
       return test;
    }
}
[/SIZE][/FONT]
```


----------



## -fool- (Dec 21, 2005)

wow that was so easy thanks.


----------



## Chicon (Jul 29, 2004)

-fool- said:


> wow that was so easy thanks.


You're welcome !


----------



## aewarnick (Sep 3, 2002)

Unfortuneatly, that code is extremely slow and inefficient. You should loop though every character in the string in the outer loop and then have an inner loop to check the vowels against the letter.


----------



## Chicon (Jul 29, 2004)

aewarnick said:


> Unfortuneatly, that code is extremely slow and inefficient. You should loop though every character in the string in the outer loop and then have an inner loop to check the vowels against the letter.


I agree that it's not the more fast. My example was meant to show some String methods and the way to create methods. The poster was using a void method to get a String data.

I've tested the code with a loop of 100000 times and an input String of 52 characters (lower and upper case alphabet) and I get a result between 3200 and 3300 milliseconds.

Here is another method that gives a result between 2110 and 2140 milliseconds :


```
[FONT="Courier New"][SIZE="4"]
  // another vowels removal method
    public String vowelRemoveBis(String test) {
      String ch;
      String res = "";
      int len = test.length();
      if (len > 0) {
        for (int i = 0; i < len; i++) {
          ch = test.substring(i,i+1);
          if (VOWELS.indexOf(ch) == -1) {
            res = res + ch;
          }
        }
      }
      return res;
  }
[/SIZE]
[/FONT]
```


----------



## Chicon (Jul 29, 2004)

-fool- said:


> wow that was so easy thanks.


Erratum : *vowel = VOWELS.substring(i,i+1)* instead of *vowel = VOWELS.substring(i, 1)*


----------



## aewarnick (Sep 3, 2002)

```
//Take note that I don't know Java.  I'm guessing at functions here.  I use C++.

public String FastVowelParse(String test)
{
	String ch;
	String res= "";
	int len= test.length();
	res.Resize(len); //resize the string here so that it isn't resized every time a vowel is appended to increase speed.
	//no need for if here
	for(int i= 0; i < len; ++i)
	{
		if(VOWELS.indexOf(test[i]) == -1)
		{
			res.Append(ch); //use some kind of append method for faster string handling
		}
	}
	return res;
}
```


----------



## Chicon (Jul 29, 2004)

Hi aewarnick,

In Java, the Resize method of the String object doens't exist.
Also, *res.concat(ch)* instead of *res.Append(ch)*.

As *test*_ is not a String object, you'll get a data type error of compilation. There is a method *toCharArray()* to cast the content of a String object into an array of character.

Short like that, I like this kind of expression *VOWELS.indexOf(test)*. :up:
Generally, I avoid them as they can easily make kilometric instructions because the verbosity of Java syntax._


----------



## aewarnick (Sep 3, 2002)

That shows how much I know about Java.


----------



## Chicon (Jul 29, 2004)

aewarnick said:


> That shows how much I know about Java.


I still have a lot to learn in Java. 

Merry Christmas, aewarnick !


----------

