From ed8c417ed570571519e9acf882119a0d34a8f079 Mon Sep 17 00:00:00 2001 From: ray Date: Wed, 29 Oct 2003 19:42:40 +0000 Subject: [PATCH] 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 --- .../java/com/samskivert/util/StringUtil.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index 084ea388..068bf46c 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -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 source with all * instances of before replaced with after.