There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
(java) I need to remove all vowels from a string


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
-fool-'s Avatar
Junior Member with 2 posts.
 
Join Date: Dec 2005
Experience: Beginner
21-Dec-2005, 02:50 AM #1
(java) I need to remove all vowels from a string
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
Code:
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's Avatar
Computer Specs
Distinguished Member with 6,553 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
21-Dec-2005, 08:04 AM #2
Quote:
Originally Posted by -fool-
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
Code:
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.
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 :

Code:

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;
    }
}
__________________
for ( ; ; ) ;

Examinations time is coming, take a Java beta exam and get a belt !

Last edited by Chicon : 21-Dec-2005 08:42 AM.
-fool-'s Avatar
Junior Member with 2 posts.
 
Join Date: Dec 2005
Experience: Beginner
21-Dec-2005, 10:30 AM #3
wow that was so easy thanks.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,553 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
21-Dec-2005, 11:55 AM #4
Quote:
Originally Posted by -fool-
wow that was so easy thanks.
You're welcome !
aewarnick's Avatar
Senior Member with 839 posts.
 
Join Date: Sep 2002
22-Dec-2005, 10:38 PM #5
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's Avatar
Computer Specs
Distinguished Member with 6,553 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
23-Dec-2005, 02:14 AM #6
Quote:
Originally Posted by aewarnick
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 :

Code:

  // 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;
  }
 
__________________
for ( ; ; ) ;

Examinations time is coming, take a Java beta exam and get a belt !

Last edited by Chicon : 23-Dec-2005 03:13 AM.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,553 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
23-Dec-2005, 02:54 AM #7
Quote:
Originally Posted by -fool-
wow that was so easy thanks.
Erratum : vowel = VOWELS.substring(i,i+1) instead of vowel = VOWELS.substring(i, 1)
aewarnick's Avatar
Senior Member with 839 posts.
 
Join Date: Sep 2002
23-Dec-2005, 09:05 AM #8
PHP Code:
//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 lentest.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 i0len; ++i)
    {
        if(
VOWELS.indexOf(test[i]) == -1)
        {
            
res.Append(ch); //use some kind of append method for faster string handling
        
}
    }
    return 
res;

Chicon's Avatar
Computer Specs
Distinguished Member with 6,553 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
23-Dec-2005, 02:44 PM #9
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[i] 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[i]).
Generally, I avoid them as they can easily make kilometric instructions because the verbosity of Java syntax.
__________________
for ( ; ; ) ;

Examinations time is coming, take a Java beta exam and get a belt !
aewarnick's Avatar
Senior Member with 839 posts.
 
Join Date: Sep 2002
23-Dec-2005, 10:30 PM #10
That shows how much I know about Java.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,553 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
25-Dec-2005, 12:09 PM #11
Quote:
Originally Posted by aewarnick
That shows how much I know about Java.
I still have a lot to learn in Java.

Merry Christmas, aewarnick !
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 02:13 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.