svn commit: r606035 - /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r606035 - /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java

jleroux@apache.org
Author: jleroux
Date: Thu Dec 20 13:49:42 2007
New Revision: 606035

URL: http://svn.apache.org/viewvc?rev=606035&view=rev
Log:
A patch from Joe Eckard "Adding new string utility methods to collapse characters" (https://issues.apache.org/jira/browse/OFBIZ-658) - OFBIZ-658

Modified:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java?rev=606035&r1=606034&r2=606035&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/StringUtil.java Thu Dec 20 13:49:42 2007
@@ -465,5 +465,47 @@
     public static String htmlSpecialChars(String html) {
         return htmlSpecialChars(html, true, true, true);
     }    
-    
+
+    /**
+     * Remove/collapse multiple newline characters
+     *
+     * @param String string to collapse newlines in
+     * @return String
+     */
+    public static String collapseNewlines(String str) {
+        return collapseCharacter(str, '\n');
+    }
+
+    /**
+     * Remove/collapse multiple spaces
+     *
+     * @param String string to collapse spaces in
+     * @return String
+     */
+    public static String collapseSpaces(String str) {
+        return collapseCharacter(str, ' ');
+    }
+
+    /**
+     * Remove/collapse multiple characters
+     *
+     * @param String string to collapse characters in
+     * @param char character to collapse
+     * @return String
+     */
+    public static String collapseCharacter(String str, char c) {
+        StringBuffer sb = new StringBuffer();
+        char last = str.charAt(0);
+
+        for (int i = 0; i < str.length(); i++) {
+            char current = str.charAt(i);
+            if (i == 0 || current != c || last != c) {
+                sb.append(current);
+                last = current;
+            }
+        }
+
+        return sb.toString();
+    }
+
 }