You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2020/07/31 05:18:43 UTC
[GitHub] [commons-lang] muriloalvesdev commented on a change in pull request #595: Add method to remove all numbers from a String
muriloalvesdev commented on a change in pull request #595:
URL: https://github.com/apache/commons-lang/pull/595#discussion_r463409756
##########
File path: src/main/java/org/apache/commons/lang3/StringUtils.java
##########
@@ -221,6 +226,27 @@ public static String abbreviate(final String str, final int maxWidth) {
return abbreviate(str, "...", 0, maxWidth);
}
+ /**
+ * <p>Removes any number contained in a String using Regex.</p>
+ *
+ * <pre>
+ * StringUtils.removeAllNumbers(null) = ""
+ * StringUtils.removeAllNumbers("") = ""
+ * StringUtils.removeAllNumbers(" ") = ""
+ * StringUtils.removeAllNumbers("123abc") = "abc"
+ * StringUtils.removeAllNumbers("123a b c") = "a b c"
+ * StringUtils.removeAllNumbers("abc123") = "abc"
+ * StringUtils.removeAllNumbers("123abc123") = "abc"
+ * StringUtils.removeAllNumbers("a1b2c3") = "abc"
+ * </pre>
+ *
+ * @param str String to check, may be null
+ * @return The String without the numbers
+ */
+ public static String removeAllNumbers(String str) {
+ return !isBlank(str) ? str.replaceAll(REGEX_FIND_NUMBERS, EMPTY) : EMPTY;
Review comment:
Hello XenoAmess!
My English is unfortunately still intermediate, did you suggest that I do this validation manually?
Was it something like that?
```
public static String removeAllNumbers(String str) {
String result = "";
if(!isEmpty(str)) {
char[] array = str.toCharArray();
for(int i = 0 ; i < array.length; i++) {
if(!Character.isDigit(array[i])) {
result += array[i];
}
}
return result;
}
return str;
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org