Repository: commons-text
Updated Branches:
  refs/heads/master e0b744e52 -> f651b78a6


Added WordUtils.abbreviate from commons lang


Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/8b42549a
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/8b42549a
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/8b42549a

Branch: refs/heads/master
Commit: 8b42549a1376ffa19b45fffbb6284c18d3cc3bc4
Parents: 0b6a285
Author: Amey Jadiye <ameyjad...@gmail.com>
Authored: Thu May 4 00:39:39 2017 +0530
Committer: Amey Jadiye <ameyjad...@gmail.com>
Committed: Thu May 4 00:41:19 2017 +0530

----------------------------------------------------------------------
 .../java/org/apache/commons/text/WordUtils.java | 69 +++++++++++++++++++-
 1 file changed, 68 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/8b42549a/src/main/java/org/apache/commons/text/WordUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/WordUtils.java 
b/src/main/java/org/apache/commons/text/WordUtils.java
index 0cee2f6..a917a1e 100644
--- a/src/main/java/org/apache/commons/text/WordUtils.java
+++ b/src/main/java/org/apache/commons/text/WordUtils.java
@@ -20,6 +20,8 @@ import java.lang.reflect.Array;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.commons.lang3.StringUtils;
+
 /**
  * <p>Operations on Strings that contain words.</p>
  *
@@ -738,4 +740,69 @@ public class WordUtils {
         return false;
     }
 
-}
+    //-----------------------------------------------------------------------
+    /**
+     * Abbreviates a string nicely.
+     *
+     * This method searches for the first space after the lower limit and 
abbreviates
+     * the String there. It will also append any String passed as a parameter
+     * to the end of the String. The upper limit can be specified to forcibly
+     * abbreviate a String.
+     *
+     * @param str         the string to be abbreviated. If null is passed, 
null is returned.
+     *                    If the empty String is passed, the empty string is 
returned.
+     * @param lower       the lower limit.
+     * @param upper       the upper limit; specify -1 if no limit is desired.
+     *                    If the upper limit is lower than the lower limit, it 
will be
+     *                    adjusted to be the same as the lower limit.
+     * @param appendToEnd String to be appended to the end of the abbreviated 
string.
+     *                    This is appended ONLY if the string was indeed 
abbreviated.
+     *                    The append does not count towards the lower or upper 
limits.
+     * @return the abbreviated String.
+     * @since 2.4
+     */
+    public static String abbreviate(String str, int lower, int upper, String 
appendToEnd) {
+        if (str == null) {
+            return null;
+        }
+
+        if (str.length() == 0) {
+            return StringUtils.EMPTY;
+        }
+
+        // if the lower value is greater than the length of the string,
+        // set to the length of the string
+        if (lower > str.length()) {
+            lower = str.length();
+        }
+
+        // if the upper value is -1 (i.e. no limit) or is greater
+        // than the length of the string, set to the length of the string
+        if (upper == -1 || upper > str.length()) {
+            upper = str.length();
+        }
+
+        // if upper is less than lower, raise it to lower
+        if (upper < lower) {
+            upper = lower;
+        }
+
+        final StringBuffer result = new StringBuffer();
+        final int index = StringUtils.indexOf(str, " ", lower);
+        if (index == -1) {
+            result.append(str.substring(0, upper));
+            // only if abbreviation has occured do we append the appendToEnd 
value
+            if (upper != str.length()) {
+                result.append(StringUtils.defaultString(appendToEnd));
+            }
+        } else if (index > upper) {
+            result.append(str.substring(0, upper));
+            result.append(StringUtils.defaultString(appendToEnd));
+        } else {
+            result.append(str.substring(0, index));
+            result.append(StringUtils.defaultString(appendToEnd));
+        }
+
+        return result.toString();
+    }
+ }

Reply via email to