commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pascalschumac...@apache.org
Subject [1/2] [lang] LANG-1307: Added getDigits method to StringUtils (closes #225)
Date Mon, 01 May 2017 09:08:18 GMT
Repository: commons-lang
Updated Branches:
  refs/heads/master c8e648b92 -> 610caab59


LANG-1307: Added getDigits method to StringUtils (closes #225)


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/9b3257e4
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/9b3257e4
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/9b3257e4

Branch: refs/heads/master
Commit: 9b3257e4d1a63e0a4d971a331eaf7b9a08d98040
Parents: c8e648b
Author: Arshad Basha <arbasha@paypal.com>
Authored: Tue Jan 3 00:14:33 2017 +0530
Committer: pascalschumacher <pascalschumacher@gmx.net>
Committed: Mon May 1 11:05:45 2017 +0200

----------------------------------------------------------------------
 .../org/apache/commons/lang3/StringUtils.java   | 36 ++++++++++++++++++++
 .../apache/commons/lang3/StringUtilsTest.java   | 11 ++++++
 2 files changed, 47 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/9b3257e4/src/main/java/org/apache/commons/lang3/StringUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index bd660bc..6ab1118 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -7157,6 +7157,42 @@ public class StringUtils {
     }
 
     /**
+     * <p>Checks if a String {@code str} contains Unicode digits,
+     * if yes then concatenate all the digits in {@code str} and return it as a String.</p>
+     *
+     * <p>An empty ("") String will be returned if no digits found in {@code str}.</p>
+     *
+     * <pre>
+     * StringUtils.getDigits(null)  = null
+     * StringUtils.getDigits("")    = ""
+     * StringUtils.getDigits("abc") = ""
+     * StringUtils.getDigits("1000$") = "1000"
+     * StringUtils.getDigits("1123~45") = "12345"
+     * StringUtils.getDigits("(541) 754-3010") = "5417543010"
+     * StringUtils.getDigits("\u0967\u0968\u0969") = "\u0967\u0968\u0969"
+     * </pre>
+     *
+     * @param str the String to extract digits from, may be null
+     * @return String with only digits,
+     *           or an empty ("") String if no digits found,
+     *           or {@code null} String if {@code str} is null
+     */
+    public static String getDigits(final String str) {
+        if (isEmpty(str)) {
+            return str;
+        }
+        final int sz = str.length();
+        final StringBuilder strDigits = new StringBuilder(sz);
+        for (int i = 0; i < sz; i++) {
+            final char tempChar = str.charAt(i);
+            if (Character.isDigit(tempChar)) {
+                strDigits.append(tempChar);
+            }
+        }
+        return strDigits.toString();
+    }
+
+    /**
      * <p>Checks if the CharSequence contains only whitespace.</p>
      * 
      * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/9b3257e4/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index b776b3b..3051b47 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -3206,4 +3206,15 @@ public class StringUtilsTest {
         assertNull(StringUtils.toCodePoints(null));
         assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, StringUtils.toCodePoints(""));
     }
+
+    @Test
+    public void testGetDigits() {
+        assertEquals(null, StringUtils.getDigits(null));
+        assertEquals("", StringUtils.getDigits(""));
+        assertEquals("", StringUtils.getDigits("abc"));
+        assertEquals("1000", StringUtils.getDigits("1000$"));
+        assertEquals("12345", StringUtils.getDigits("123password45"));
+        assertEquals("5417543010", StringUtils.getDigits("(541) 754-3010"));
+        assertEquals("\u0967\u0968\u0969", StringUtils.getDigits("\u0967\u0968\u0969"));
+    }
 }


Mime
View raw message