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/11/13 19:13:53 UTC
[GitHub] [commons-lang] garydgregory commented on a change in pull request #646: [LANG-1345] - Add methods to enhance non-empty strings
garydgregory commented on a change in pull request #646:
URL: https://github.com/apache/commons-lang/pull/646#discussion_r523169334
##########
File path: src/main/java/org/apache/commons/lang3/StringUtils.java
##########
@@ -7274,6 +7274,52 @@ public static String rightPad(final String str, final int size, String padStr) {
}
}
+ /**
+ * <p>Returns either the passed in String with the specified prefix and suffix attached,
+ * or if the String is whitespace, empty ("") or {@code null}, an empty string.</p>
+ *
+ * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
+ *
+ * <pre>
+ * StringUtils.defaultIfNotBlank(null, "pre-", "-post") = ""
+ * StringUtils.defaultIfNotBlank("", "pre-", "-post") = ""
+ * StringUtils.defaultIfNotBlank(" ", "pre-", "-post") = ""
+ * StringUtils.defaultIfNotBlank("bat", "pre-", "-post") = "pre-bat-bost"
+ * StringUtils.defaultIfNotBlank("bat", null, "-post") = "bat-post"
+ * StringUtils.defaultIfNotBlank("bat", "pre-", null) = "pre-bat"
+ * </pre>
+ * @param str the String to check, may be null
+ * @param prefix the string to prepend if not blank. Null will be converted to empty string.
+ * @param suffix the string to append if not blank. Null will be converted to empty string.
+ * @return the passed in String with prefix and suffix added, or empty string
+ * @see StringUtils#defaultString(String, String)
+ */
Review comment:
And since tag.
##########
File path: src/main/java/org/apache/commons/lang3/StringUtils.java
##########
@@ -7274,6 +7274,52 @@ public static String rightPad(final String str, final int size, String padStr) {
}
}
+ /**
+ * <p>Returns either the passed in String with the specified prefix and suffix attached,
+ * or if the String is whitespace, empty ("") or {@code null}, an empty string.</p>
+ *
+ * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
+ *
+ * <pre>
+ * StringUtils.defaultIfNotBlank(null, "pre-", "-post") = ""
+ * StringUtils.defaultIfNotBlank("", "pre-", "-post") = ""
+ * StringUtils.defaultIfNotBlank(" ", "pre-", "-post") = ""
+ * StringUtils.defaultIfNotBlank("bat", "pre-", "-post") = "pre-bat-bost"
+ * StringUtils.defaultIfNotBlank("bat", null, "-post") = "bat-post"
+ * StringUtils.defaultIfNotBlank("bat", "pre-", null) = "pre-bat"
+ * </pre>
+ * @param str the String to check, may be null
+ * @param prefix the string to prepend if not blank. Null will be converted to empty string.
+ * @param suffix the string to append if not blank. Null will be converted to empty string.
+ * @return the passed in String with prefix and suffix added, or empty string
+ * @see StringUtils#defaultString(String, String)
+ */
+ public static String defaultIfNotBlank(final String str, final String prefix, final String suffix) {
+ return isBlank(str) ? "" : defaultString(prefix) + str + defaultString(suffix);
Review comment:
Use EMPTY constant.
----------------------------------------------------------------
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