Results 1 to 8 of 8
- 06-10-2012, 10:21 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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:
For example, if string was:Java Code:while(string.hasMoreTokens()){ word = string.nextToken().toLowerCase().replaceAll("[^a-z]", ""); System.out.print(word + " "); }
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 10:51 AM.
-
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.
- 06-10-2012, 01:57 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
-
Re: Remove all punctuation from a string?
- 06-10-2012, 02:54 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Remove all punctuation from a string?
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-11-2012, 08:54 AM #6
Banned
- Join Date
- Jun 2012
- Location
- Beijing,China
- Posts
- 34
- Rep Power
- 0
Re: Remove all punctuation from a string?
Maybe you need two replaceAll() . First is for lady's , Second is for another non-alpha character.
- 06-11-2012, 09:20 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Remove all punctuation from a string?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-11-2012, 09:37 AM #8
Banned
- Join Date
- Jun 2012
- Location
- Beijing,China
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
Remove part of a string
By PhQ in forum New To JavaReplies: 2Last Post: 09-16-2011, 11:52 PM -
How to remove numberformatexception for string??
By lkalaivanan in forum New To JavaReplies: 3Last Post: 08-11-2011, 05:38 AM -
How to remove comma before an String?
By bhba73 in forum New To JavaReplies: 14Last Post: 07-28-2011, 04:33 PM -
Split string and remove
By drogba123 in forum New To JavaReplies: 2Last Post: 03-20-2011, 10:52 AM -
remove all punctuation except dots hyphens and spaces with a regular expression
By Daedalus in forum Advanced JavaReplies: 2Last Post: 01-05-2011, 06:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks