[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Remove all punctuation!!!

Posted on 2004-05-01
15
Medium Priority
?
697 Views
Last Modified: 2006-11-17
Hi,

 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
0
Comment
Question by:garth15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 7

Accepted Solution

by:
maheshexp earned 2000 total points
ID: 10970039
      while (words.hasMoreTokens())
       {
           String word1 = words.nextToken();
            word1 = word1.replaceAll(".","");
            word1 = word1.replaceAll("!","");
           /* other characters to be replaced */

           Chat[i] = words1;
           i++;
       }
       
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970042
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970051
String str1 = "hai.how are you, my friend?";
            String[] arr = str1.split(".?, ");
            StringTokenizer st = new StringTokenizer(str1,".?!, ");
            while(st.hasMoreTokens()){
                  System.out.println(st.nextToken());
            }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:maheshexp
ID: 10970052
String str1 = "hai.how are you, my friend?";
StringTokenizer st = new StringTokenizer(str1,".?!, ");

while(st.hasMoreTokens()){
      System.out.println(st.nextToken());
}
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970058
in the 3rd post remove this lie  String[] arr = str1.split(".?, ");

u can also split using Regular Expressions
            String str1 = "hai.how are you, my friend?";
            String[] arr = str1.split("[.?, ]");
            for (int i = 0; i < arr.length; i++) {
                  System.out.println(arr[i]);
            }
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970065
hope u got it
0
 

Author Comment

by:garth15
ID: 10971121
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?
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10971227
> based on ASCII numbers so that all non
what do u mean by this
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10971232
does you sentance have \' literal....
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10971248
>> 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.
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10971316
               String pattern = "(\\p{Alpha}*)(\\p{Punct}*)(\\p{Digit}*)";
            
            String text = "hello??";
            
            String[] sp = text.split(pattern);
            Pattern pat = Pattern.compile(pattern);
            Matcher match = pat.matcher(text);
            System.out.println(match.matches());
            System.out.println(match.groupCount());
            
            if(match.matches())
            for(int i = 0; i <= match.groupCount(); i++){
                  System.out.println( i + ":" + match.group(i));
            }
0
 
LVL 92

Expert Comment

by:objects
ID: 10974040
if you use a PreparedStatement to do your query then there is no need to remove punctuation.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question