/**
* Created on Apr 12, 2007
* @author Kushal Paudyal
* www.sanjaal.com/java
*/
/**
* This utility class has method to remove punctuation
* marks from a given string
*/
public class StringCleaner {
private static String legalCharacterSet;
/**
* Default Constructor
* Create a string that contains standard legal characters.
* This string will be used to reference whether the
* characters in any 'to be cleaned'
* string are to be kept or not.
*/
public StringCleaner()
{
legalCharacterSet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
"abcdefghijklmnopqrstuvwxyz"+
"1234567890-";
}
/**
* We may provide our own set of valid characters which
* are to be used on testing the validity.
* This set is provided as a string containing
* valid characters.Order is not important.
* @param validCharacterSet
*/
public StringCleaner(String validCharacterSet )
{
this.legalCharacterSet=validCharacterSet;
}
/**
* @param str The string to be cleaned
* (unnecessary characters and whitespaces removed)
* @return Cleaned String
*/
public String cleanString(String str)
{
String cleanedString="";
for(int index=0;index
{
char currentCharacter=str.charAt(index);
if(legalCharacterSet.indexOf(currentCharacter)>=0)
cleanedString+=currentCharacter;
}
return cleanedString;
}
}
Originally posted 2007-09-13 15:42:29.