Oh, the proliferation of crap. Added a method sanitize() which filters
bogus characters from a String. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1282 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: StringUtil.java,v 1.60 2003/10/24 01:17:51 ray Exp $
|
||||
// $Id: StringUtil.java,v 1.61 2003/10/29 19:42:40 ray Exp $
|
||||
//
|
||||
// samskivert library - useful routines for java programs
|
||||
// Copyright (C) 2001 Michael Bayne
|
||||
@@ -68,6 +68,30 @@ public class StringUtil
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a character.
|
||||
*/
|
||||
public static interface CharacterValidator
|
||||
{
|
||||
public boolean isValid (char c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the specified String so that only valid characters are in it.
|
||||
*/
|
||||
public static String sanitize (String source, CharacterValidator validator)
|
||||
{
|
||||
int nn = source.length();
|
||||
StringBuffer buf = new StringBuffer(nn);
|
||||
for (int ii=0; ii < nn; ii++) {
|
||||
char c = source.charAt(ii);
|
||||
if (validator.isValid(c)) {
|
||||
buf.append(c);
|
||||
}
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string based on <code>source</code> with all
|
||||
* instances of <code>before</code> replaced with <code>after</code>.
|
||||
|
||||
Reference in New Issue
Block a user