Results 1 to 8 of 8
  1. #1
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Remove all punctuation from a string?

    I need to convert a string of several words/symbols into several seperate strings comprising only lower case letters.

    At the moment I am using string tokenizer class and the replaceAll method:

    Java Code:
    while(string.hasMoreTokens()){
    	word = string.nextToken().toLowerCase().replaceAll("[^a-z]", "");
    	System.out.print(word + " ");
    }
    For example, if string was:

    The boys ring the old lady's bell. Ding, dong!Then they run off so she cannot see them.

    ..output should be:

    the boys ring the old lady s bell ding dong then they run off so she cannot see them

    ..but I am getting:

    the boys ring the old ladys bell ding dongthen they run off so she cannot see them


    Any advice is appreciated.
    Last edited by cherrychives; 06-10-2012 at 11:51 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Remove all punctuation from a string?

    I would not use a String tokenizer but simply use String#replaceAll(...). I would use a greedy quantifier that takes all non-character Strings, and replaces it with a space.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Remove all punctuation from a string?

    Quote Originally Posted by Fubarable View Post
    I would not use a String tokenizer but simply use String#replaceAll(...). I would use a greedy quantifier that takes all non-character Strings, and replaces it with a space.
    And in a second pass I'd replace multiple spaces by a single space character.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Remove all punctuation from a string?

    Quote Originally Posted by JosAH View Post
    And in a second pass I'd replace multiple spaces by a single space character.
    I don't think that there's a need to do that since the greedy quantifier should grab all spaces and replace with a single space on the first pass.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Remove all punctuation from a string?

    Quote Originally Posted by Fubarable View Post
    I don't think that there's a need to do that since the greedy quantifier should grab all spaces and replace with a single space on the first pass.
    Yes, o course; I misread your answer (it's Sunday) and I immediately put on my Mr-Wise-Guy hat and posted my reply; sorry about that; I'm sitting in the sun with my laptop right now and I can hardly read what is displayed on that shiny schreen. I liked those dim screens much better ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Remove all punctuation from a string?

    Maybe you need two replaceAll() . First is for lady's , Second is for another non-alpha character.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Remove all punctuation from a string?

    Quote Originally Posted by Ksharp View Post
    Maybe you need two replaceAll() . First is for lady's , Second is for another non-alpha character.
    That is not neeed (as Fubarable already correctly pointed out above); a single replacement can do the job:

    Java Code:
    String result= yourString.toLowerCase().replaceAll("[^a-z]+", " ");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Remove all punctuation from a string?

    Sorry. Misunderstanding.
    I took the output OP want as the wrong output. I think I am old now. :(

Similar Threads

  1. Remove part of a string
    By PhQ in forum New To Java
    Replies: 2
    Last Post: 09-17-2011, 12:52 AM
  2. How to remove numberformatexception for string??
    By lkalaivanan in forum New To Java
    Replies: 3
    Last Post: 08-11-2011, 06:38 AM
  3. How to remove comma before an String?
    By bhba73 in forum New To Java
    Replies: 14
    Last Post: 07-28-2011, 05:33 PM
  4. Split string and remove
    By drogba123 in forum New To Java
    Replies: 2
    Last Post: 03-20-2011, 11:52 AM
  5. Replies: 2
    Last Post: 01-05-2011, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •