Main Topics
Browse All TopicsHi,
I have the following, very simple class that seperates strings in to words. I then use these words as part of an SQL statement to look up their meaning. I really need to remove all the punctuation so that the SQL statement runs. And also so words are looked up without such characters as "£*!. at the end. Can anyone show me how to write a simple method to do this please.
import java.util.StringTokenizer;
public class SeperateWords {
public DBConnection dbConnection=null;
/** Creates a new instance of SeperateWords */
public SeperateWords(String chat)
{
StringTokenizer words = new StringTokenizer(chat);
String[] Chat = new String[words.countTokens()
int i=0;
while (words.hasMoreTokens())
{
Chat[i] = words.nextToken().toString
i++;
}
dbConnection = new DBConnection(Chat);
}
}
Thanks
Garth
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Is there any way to remove all punctuation based on ASCII numbers so that all non alpha-numeric characters are removed from each word? I had already looked at the string tokenizer way but my statement was massive!! Also I have problems with the ' character as it thinks it begins or ends a string literal. Any suggestions?
>> all non alpha-numeric characters are removed
Try this:
public String removeChars ( String sSource )
{
StringBuffer sbTemp = new StringBuffer ( sSource ) ;
for ( int i = sbTemp.length () - 1 ; i >= 0 ; i -- )
if ( ! Character.isLetterOrDigit ( sbTemp.charAt ( i ) ) )
sbTemp.deleteCharAt ( i ) ; // end if, for
return sbTemp.toString () ;
}
Pass the word to it. It should return a word containing only alphabets/ digits, with the other characters removed.
String pattern = "(\\p{Alpha}*)(\\p{Punct}*
String text = "hello??";
String[] sp = text.split(pattern);
Pattern pat = Pattern.compile(pattern);
Matcher match = pat.matcher(text);
System.out.println(match.m
System.out.println(match.g
if(match.matches())
for(int i = 0; i <= match.groupCount(); i++){
System.out.println( i + ":" + match.group(i));
}
Business Accounts
Answer for Membership
by: maheshexpPosted on 2004-05-01 at 20:05:35ID: 10970039
while (words.hasMoreTokens())
{
String word1 = words.nextToken();
word1 = word1.replaceAll(".","");
word1 = word1.replaceAll("!","");
/* other characters to be replaced */
Chat[i] = words1;
i++;
}