Removing punctuation from the beginning and end of a string
807591Apr 30 2008 — edited Apr 30 2008I am working on a project for a "spell checking" program. So far the StringHash class is made, and the dictionary is read into a hash table. Then the document to be checked is specified via command line, the document scans through each word and checks to see if it is in the dictionary, if it is do nothing, if it is not then display it to console. The problem I am having is with a method in the main class. The method (I will post what I have so far) is called removePunct. The method should remove any punctuation from the beginning and the end of the string, nothing from the middle. Here is what I have so far:
public static String removePunct(String word){
StringBuilder wo = new StringBuilder(word);
int x = (word.length()-1);
if(!Character.isLetter(wo.charAt(0)))
wo.deleteCharAt(0);
if(!Character.isLetter(wo.charAt(x)))
wo.deleteCharAt(x);
return wo.toString();
}
Any help would be much appreciated, I'm pretty certain the solution is either something completely different, or something so simple that I just missed it.