[
https://issues.apache.org/jira/browse/LANG-1345?focusedWorklogId=511497&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-511497
]
ASF GitHub Bot logged work on LANG-1345:
----------------------------------------
Author: ASF GitHub Bot
Created on: 13/Nov/20 19:13
Start Date: 13/Nov/20 19:13
Worklog Time Spent: 10m
Work Description: 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:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 511497)
Remaining Estimate: 23.5h (was: 23h 40m)
Time Spent: 0.5h (was: 20m)
> Add methods to enhance non-empty strings
> ----------------------------------------
>
> Key: LANG-1345
> URL: https://issues.apache.org/jira/browse/LANG-1345
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.*
> Reporter: Mitth'raw'nuruodo
> Priority: Minor
> Original Estimate: 24h
> Time Spent: 0.5h
> Remaining Estimate: 23.5h
>
> As a corollary to {{defaultString}}, {{appendIfMissing}}, and similar
> methods, which allow the client to replace a missing string with something,
> it would sometimes be helpful to have methods that enhance a string, if it is
> present, with a prefix and suffix (or perhaps with an arbitrary function).
> Eg
> {code}
> if (StringUtils.isNotEmpty(queryString)) {
> return url + "?" + queryString;
> } else {
> return url;
> }
> {code}
> would become:
> {code}
> return url + StringUtils.addIfNotEmpty(queryString, "?", null);
> {code}
> with the second parameter being the prefix, and the third, the suffix. If the
> string is null or empty, return empty string.
> If this could accept arbitrary transformation functions, it could effectively
> become the Elvis operator.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)