This discussion is archived
1 2 3 Previous Next 37 Replies Latest reply: May 14, 2008 2:16 PM by 800282 RSS

Help! Removing all punctuation from a string

807591 Newbie
Currently Being Moderated
I'm trying to write some code that takes a string and removes all punctuation from the string. I think i would use the method removeDelimiters, but i can't seem to get that to work. Could someone give me some sample code for this.
  • 1. Re: Help! Removing all punctuation from a string
    807591 Newbie
    Currently Being Moderated
    one way is:
    yourString = yourString.replaceAll(",", "").replaceAll(".", "").replaceAll(etc...);
    but it's not a good idea , you will have to call the replaceAll method too many times ; another way is to create a method:
    public String removePunctuation(String input) {}
    in that method, you :

    1/ create a new stringbuilder
    2/ for each character of the input string
    3/ if character is ponctuation, do nothing, else, append the character to the stringbuilder
    4/ return the stringbuilder as string
  • 2. Re: Help! Removing all punctuation from a string
    807591 Newbie
    Currently Being Moderated
    Sorry 'calvino_ind' but your replaceAll() code is fatally flawed (it will always produce an empty string) and unnecessarily complicated. The easy approach
    String stringWithoutPunctuation = stringWithPunctuation.replaceAll("\\p{Punct}+","");
    If this set of punctuation is to large then it is easy enough to create a smaller set.
  • 3. Re: Help! Removing all punctuation from a string
    807591 Newbie
    Currently Being Moderated
    @sabre : actually , it didn't even give an empty string, but an execution error, the thing was more:
     System.out.println("Hey, mate, how are you?".replaceAll("\\,", "").replaceAll("\\?", ""));
    but your pattern is the real thing, for sure :)
  • 4. Re: Help! Removing all punctuation from a string
    807591 Newbie
    Currently Being Moderated
    calvino_ind wrote:
    @sabre : actually , it didn't even give an empty string, but an execution error, the thing was more:
    Since you did not show all your test code I can't comment on the execution error but I can say that
    replaceAll(".","")
    will always produce an empty string.

    Edit: I forgot - except it will not remove end-of-line characters.

    Edited by: sabre150 on May 14, 2008 11:59 AM
  • 5. Re: Help! Removing all punctuation from a string
    807591 Newbie
    Currently Being Moderated
    sabre150 wrote:
    If this set of punctuation is to large then it is easy enough to create a smaller set.
    :-( s/to/too/
  • 6. Re: Help! Removing all punctuation from a string
    807591 Newbie
    Currently Being Moderated
    public String removeChar(String str, int pos)   
    {       
           String newstr="";        
           for(int i = 0; i<str.length(); i++)        
           {            
                 if(i != pos)                
                    newstr += str.charAt(i);        
            }        
            return newstr;
    }
    
    public String removePunct(String str)
    {
           for( int i = 0; i < str.length(); i++)
           {
                  if((str.charAt(i) < 'a' && str.charAt(i) > 'z')
                  {
                       if(str.charAt(i) < 'A' && str.char(i) >'Z')  //str.charAt(i) < A something glitching
                       {
                            if(str.charAt(i) < '0' && str.charAt(i) > '9')
                            {
                                if(str.charAt(i) != ' ') //so it doesn't remove spaces. 
                                {
                                     str = removeChar(str, i);
                                     i--;
                                }
                            }
                      }
                }
            }
            return str;
    }
    You could just make one big if statement but this looks more clear, hope it helps :)

    Warning not at home right now, and did not write this code on a compiler, there maybe errors, but you should get the idea

    Edited by: houseurmusic on May 14, 2008 1:36 PM
  • 7. Re: Help! Removing all punctuation from a string
    800472 Newbie
    Currently Being Moderated
    houseurmusic wrote:
    public String removeChar(String str, int pos)   
    {       
    String newstr="";        
    for(int i = 0; i<str.length(); i++)        
    {            
    if(i != pos)                
    newstr += str.charAt(i);        
    }        
    return newstr;
    }
    
    public String removePunct(String str)
    {
    for( int i = 0; i < str.length(); i++)
    {
    if((str.charAt(i) < 'a' && str.charAt(i) > 'z')
    {
    if(str.charAt(i) < 'A' && str.char(i) >'Z')  //str.charAt(i) < A something glitching
    {
    if(str.charAt(i) < '0' && str.charAt(i) > '9')
    {
    if(str.charAt(i) != ' ') //so it doesn't remove spaces. 
    {
    str = removeChar(str, i);
    i--;
    }
    }
    }
    }
    }
    return str;
    }
    You could just make one big if statement but this looks more clear, hope it helps :)

    Warning not at home right now, and did not write this code on a compiler, there maybe errors, but you should get the idea

    Edited by: houseurmusic on May 14, 2008 1:36 PM
    If that's your definition of clear, I'd hate to see your muddy code.
  • 8. Re: Help! Removing all punctuation from a string
    800282 Newbie
    Currently Being Moderated
    houseurmusic wrote:
    public String removeChar(String str, int pos)   
    {       
    String newstr="";        
    for(int i = 0; i<str.length(); i++)        
    {            
    if(i != pos)                
    newstr += str.charAt(i);        
    }        
    return newstr;
    }
    
    public String removePunct(String str)
    {
    for( int i = 0; i < str.length(); i++)
    {
    if((str.charAt(i) < 'a' && str.charAt(i) > 'z')
    {
    if(str.charAt(i) < 'A' && str.char(i) >'Z')  //str.charAt(i) < A something glitching
    {
    if(str.charAt(i) < '0' && str.charAt(i) > '9')
    {
    if(str.charAt(i) != ' ') //so it doesn't remove spaces. 
    {
    str = removeChar(str, i);
    i--;
    }
    }
    }
    }
    }
    return str;
    }
    You could just make one big if statement but this looks more clear, hope it helps :)

    Warning not at home right now, and did not write this code on a compiler, there maybe errors, but you should get the idea

    Edited by: houseurmusic on May 14, 2008 1:36 PM
    I was hoping you were making a joke... But it appears you are serious. Well, that code is, err, bad. Really bad.

    You wrote your own removeChar(...) method where you know the index of the char you want to remove, but you are still looping through the entire String! besides that, String has various replace(...) methods to do exactly that.
    And your idea (since the code will never compile), will also mark characters like '�' as a punctuation mark.
    And all those nested loops and if statements make my eyes bleed!
    And...

    Did you not notice the solution posted by sabre? That is the one the OP should use!
  • 9. Re: Help! Removing all punctuation from a string
    807591 Newbie
    Currently Being Moderated
    hmm don't see anything wrong with it. How would you suggest a better solution, a case statement for punctations would work I guess, I don't know what he might consider punctuations. this function would remove everything thats not a letter space or number, so the user may still want certain symbols in wich case I would suggest a switch statement.
  • 10. Re: Help! Removing all punctuation from a string
    796440 Guru
    Currently Being Moderated
    nclow wrote:
    If that's your definition of clear, I'd hate to see your muddy code.
    Indeed.

    It's also terribly wasteful. It creates a new String for every character being kept in the original string every time a character is removed.
  • 11. Re: Help! Removing all punctuation from a string
    807591 Newbie
    Currently Being Moderated
    lol how do you think the String class does it?

    sabre posted a good solution if he wants to rely on the libray, but in that case I don't know why the op would be asking such a question.
    You guys have been programming to long, and forget some people are interested in how it works rather than calling a magic function to do the work.
  • 12. Re: Help! Removing all punctuation from a string
    807591 Newbie
    Currently Being Moderated
    Damn didn't know I would get so critisized on a simple solution. Wasn't really going to sit here and think for hours what the most effecient way to do this is.

    Yes you could write a algoritm that would look through a string find all the punctions, and store each index of that punctuation in a array. from there you could make a new string by copying each character and skipping each index that was marked. I really don't feel like writing that.
  • 13. Re: Help! Removing all punctuation from a string
    796440 Guru
    Currently Being Moderated
    houseurmusic wrote:
    hmm don't see anything wrong with it.
    Nested ifs are hard to follow.
    Duplicating functionality that already exists in String/StringBuilder/StringBuffer.
    Horribly wasteful.
    How would you suggest a better solution,
    Not doing those things.
  • 14. Re: Help! Removing all punctuation from a string
    796440 Guru
    Currently Being Moderated
    houseurmusic wrote:
    lol how do you think the String class does it?
    Absolutely not the way you did, I guarantee.
    sabre posted a good solution if he wants to rely on the libray
    Which one should do.
    You guys have been programming to long, and forget some people are interested in how it works rather than calling a magic function to do the work.
    Nobody has forgotten that. If the OP wants to know how to do it himself, he need only ask, and somebody will help him find a far better solution than what you posted.
1 2 3 Previous Next