From issues-return-65918-archive-asf-public=cust-asf.ponee.io@commons.apache.org Fri Jan 12 18:15:37 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id 6164C180621 for ; Fri, 12 Jan 2018 18:15:37 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 5176B160C42; Fri, 12 Jan 2018 17:15:37 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 98D16160C30 for ; Fri, 12 Jan 2018 18:15:36 +0100 (CET) Received: (qmail 76729 invoked by uid 500); 12 Jan 2018 17:15:35 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 76698 invoked by uid 99); 12 Jan 2018 17:15:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Jan 2018 17:15:35 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 5B128DFCB6; Fri, 12 Jan 2018 17:15:35 +0000 (UTC) From: garydgregory To: issues@commons.apache.org Reply-To: issues@commons.apache.org References: In-Reply-To: Subject: [GitHub] commons-lang pull request #278: Lang-1345 Enhance non-empty strings Content-Type: text/plain Message-Id: <20180112171535.5B128DFCB6@git1-us-west.apache.org> Date: Fri, 12 Jan 2018 17:15:35 +0000 (UTC) Github user garydgregory commented on a diff in the pull request: https://github.com/apache/commons-lang/pull/278#discussion_r161277512 --- Diff: src/main/java/org/apache/commons/lang3/StringUtils.java --- @@ -7429,6 +7429,55 @@ public static String defaultString(final String str, final String defaultStr) { return isEmpty(str) ? defaultStr : str; } + // Extensions + //----------------------------------------------------------------------- + + /** + *

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.

+ * + *

Whitespace is defined by {@link Character#isWhitespace(char)}.

+ * + *
    +     * StringUtils.extendIfNotBlank(null, "pre-", "-post")  = ""
    +     * StringUtils.extendIfNotBlank("", "pre-", "-post")    = ""
    +     * StringUtils.extendIfNotBlank(" ", "pre-", "-post")   = ""
    +     * StringUtils.extendIfNotBlank("bat", "pre-", "-post") = "pre-bat-bost"
    +     * StringUtils.extendIfNotBlank("bat", null, "-post")      = "bat-post"
    +     * StringUtils.extendIfNotBlank("bat", "pre-", null)      = "pre-bat"
    +     * 
+ * @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 extendIfNotBlank(final String str, final String prefix, final String suffix) { + return isBlank(str) ? "" : defaultString(prefix) + str + defaultString(suffix); --- End diff -- Reuse the EMPTY constant. ---