Author: sichen
Date: Fri Jul 21 15:33:45 2006
New Revision: 424482
URL:
http://svn.apache.org/viewvc?rev=424482&view=revLog:
New remove Regex from string methods in StringUtil, with reimplementation of the removeNonNumeric and removeNumeric. From Andrew Sykes. OFBIZ-73
Modified:
incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java
Modified: incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java
URL:
http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java?rev=424482&r1=424481&r2=424482&view=diff==============================================================================
--- incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java (original)
+++ incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java Fri Jul 21 15:33:45 2006
@@ -25,6 +25,8 @@
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* Misc String Utility Functions
@@ -260,15 +262,10 @@
/** Removes all spaces from a string */
public static String removeSpaces(String str) {
- StringBuffer newString = new StringBuffer();
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) != ' ')
- newString.append(str.charAt(i));
- }
- return newString.toString();
+ return removeRegex(str,"[\\d]");
}
- public static String toHexString(byte[] bytes) {
+ public static String toHexString(byte[] bytes) {
StringBuffer buf = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
buf.append(hexChar[(bytes[i] & 0xf0) >>> 4]);
@@ -328,4 +325,25 @@
} while (i != 0);
return digestChars;
}
+
+ /** Removes all non-numbers from str */
+ public static String removeNonNumeric(String str) {
+ return removeRegex(str,"[\\D]");
+ }
+
+ /** Removes all numbers from str */
+ public static String removeNumeric(String str) {
+ return removeRegex(str,"[\\d]");
+ }
+
+ /**
+ * @param str
+ * @param regex
+ * Removes all matches of regex from a str
+ */
+ private static String removeRegex(String str, String regex) {
+ Pattern pattern = Pattern.compile(regex);
+ Matcher matcher = pattern.matcher(str);
+ return matcher.replaceAll("");
+ }
}