Cleaning Punctuation And White Spaces From A String
Kushal Paudyal May 17th, 2010
/**
* 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;
}
}
Sanjaal.com is owned and maintained by Sanjaal Corps, Nepal. The company offers Webhosting and Domain Registration Services, IT Solutions and Business Analysis. Sanjaal.com website features H1B Visa Information, Entertainment Portal, Link Directory Service, Free Articles, Free Open Source Tutorials on Java and J2EE Platform, Digital Photography, High Resolution Picture Gallery and Free Reliable Image Hosting Services. Future plan includes Open Source Software Development Portal, Technical Solutions and Customizable Movie and Music Arena. We would be introducing data backup, data recovery, data hosting and voip solutions. Stay free from phishing – our website does not ask for your credit card and banking information. Happy Surfing!
Originally posted 2007-09-13 15:42:29.