All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.imsejin.common.util.StringUtils Maven / Gradle / Ivy

package io.github.imsejin.common.util;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String utilities
 */
public final class StringUtils {

    /**
     * Whitespace character.
     */
    private static final char WHITE_SPACE = '\u0020';

    private StringUtils() {
    }

    /**
     * Checks whether the string is null or empty.
     *
     * 
{@code
     *     isNullOrEmpty(null);     // true
     *     isNullOrEmpty("");       // true
     *     isNullOrEmpty("abc");    // false
     * }
* * @param str string * @return whether the string is null or empty */ public static boolean isNullOrEmpty(String str) { return str == null || str.isEmpty(); } /** * If the string is null or empty, this returns default string. *
* If not, this returns original string. * *
{@code
     *     ifNullOrEmpty(null, "(empty)");      // (empty)
     *     ifNullOrEmpty("", "(empty)");        // (empty)
     *     ifNullOrEmpty(" ", "(empty)");       // \u0020
     * }
* * @param str original string * @param defaultValue default string * @return original string or default string */ public static String ifNullOrEmpty(String str, String defaultValue) { return isNullOrEmpty(str) ? defaultValue : str; } /** * If the string is null or empty, this returns default string. *
* If not, this returns original string. * *
{@code
     *     ifNullOrEmpty(null, () -> "(empty)");    // (empty)
     *     ifNullOrEmpty("", () -> "(empty)");      // (empty)
     *     ifNullOrEmpty(" ", () -> "(empty)");     // \u0020
     * }
* * @param str original string * @param supplier supplier that returns default string * @return original string or default string */ public static String ifNullOrEmpty(String str, Supplier supplier) { return isNullOrEmpty(str) ? supplier.get() : str; } /** * Checks whether the string is null or blank. * *
{@code
     *     isNullOrBlank(null);     // true
     *     isNullOrBlank("");       // true
     *     isNullOrBlank(" ");      // true
     *     isNullOrBlank(" ABC");   // false
     * }
* * @param str string * @return whether the string is null or blank */ public static boolean isNullOrBlank(String str) { return str == null || str.trim().isEmpty(); } /** * If the string is null or blank, this returns default string. *
* If not, this returns original string. * *
{@code
     *     ifNullOrBlank(null, "(empty)");      // (empty)
     *     ifNullOrBlank("", "(empty)");        // (empty)
     *     ifNullOrBlank(" ", "(empty)");       // (empty)
     *     ifNullOrBlank(" ABC", "(empty)");    //  ABC
     * }
* * @param str original string * @param defaultValue default string * @return original string or default string */ public static String ifNullOrBlank(String str, String defaultValue) { return isNullOrBlank(str) ? defaultValue : str; } /** * If the string is null or blank, this returns default string. *
* If not, this returns original string. * *
{@code
     *     ifNullOrBlank(null, () -> "(empty)");    // (empty)
     *     ifNullOrBlank("", () -> "(empty)");      // (empty)
     *     ifNullOrBlank(" ", () -> "(empty)");     // (empty)
     *     ifNullOrBlank(" ABC", () -> "(empty)");  //  ABC
     * }
* * @param str original string * @param supplier supplier that returns default string * @return original string or default string */ public static String ifNullOrBlank(String str, Supplier supplier) { return isNullOrBlank(str) ? supplier.get() : str; } /** * 공백 문자열이 하나라도 있는지 확인한다. * *
{@code
     *     anyNullOrBlank(null, " ");       // true
     *     anyNullOrBlank(null, "ABC");     // true
     *     anyNullOrBlank("ABC", "");       // true
     *     anyNullOrBlank(" ", "ABC");      // true
     *     anyNullOrBlank(" ABC", "ABC");   // false
     * }
* * @param strs strings * @return whether any strings are null or blank */ public static boolean anyNullOrBlank(String... strs) { if (strs == null || strs.length == 0) return true; for (String str : strs) { if (isNullOrBlank(str)) return true; } return false; } /** * 모두 공백 문자열인지 확인한다. * *
{@code
     *     allNullOrBlank(null, " ");       // true
     *     allNullOrBlank(null, "ABC");     // false
     *     allNullOrBlank("ABC", "");       // false
     *     allNullOrBlank(" ", "ABC");      // false
     *     allNullOrBlank(" ABC", "ABC");   // false
     * }
* * @param strs strings * @return whether all strings are null or blank */ public static boolean allNullOrBlank(String... strs) { if (strs == null || strs.length == 0) return true; return Arrays.stream(strs).allMatch(StringUtils::isNullOrBlank); } /** * `기준 문자열`과 일치하는 문자열이 하나라도 있는지 확인한다. * *
{@code
     *     anyEquals (null, null);          // false
     *     anyEquals("", null);             // false
     *     anyEquals(null, "");             // false
     *     anyEquals("", null, "");         // true
     *     anyEquals("ABC", "abc");         // false
     *     anyEquals("ABC", "abc", "ABC");  // true
     * }
* * @param criterion criterion string * @param strs strings * @return whether any strings are equal to criterion string */ public static boolean anyEquals(String criterion, String... strs) { if (criterion == null || strs == null || strs.length == 0) return false; for (String str : strs) { if (criterion.equals(str)) return true; } return false; } /** * Fills the start of string with whitespace. * *
{@code
     *     padStart(8, "0304"); // "    0304"
     *     padStart(4, "0304"); // "0304"
     * }
* * @param len targeted string length; * If it is less than the length of origin string, * this returns it as it is. * @param origin string that needs to be padded * @return padded string * @see #repeat(String, int) */ public static String padStart(int len, String origin) { return padStart(len, origin, String.valueOf(WHITE_SPACE)); } /** * Fills the start of string with another string. * *
{@code
     *     padStart(8, "0304", "0"); // "00000304"
     *     padStart(4, "0304", "0"); // "0304"
     * }
* * @param len targeted string length; * If it is less than the length of origin string, * this returns it as it is. * @param origin string that needs to be padded * @param appendix string to be appended to origin string * @return padded string * @see #repeat(String, int) */ public static String padStart(int len, String origin, String appendix) { int originLen = origin.length(); if (originLen >= len) return origin; return repeat(appendix, len - originLen) + origin; } /** * Fills the end of string with whitespace. * *
{@code
     *     padEnd(8, "0304"); // "0304    "
     *     padEnd(4, "0304"); // "0304"
     * }
* * @param len targeted string length; * If it is less than the length of origin string, * this returns it as it is. * @param origin string that needs to be padded * @return padded string * @see #repeat(String, int) */ public static String padEnd(int len, String origin) { return padEnd(len, origin, String.valueOf(WHITE_SPACE)); } /** * Fills the end of string with another string. * *
{@code
     *     padEnd(8, "0304", "0"); // "03040000"
     *     padEnd(4, "0304", "0"); // "0304"
     * }
* * @param len targeted string length; * If it is less than the length of origin string, * this returns it as it is. * @param origin string that needs to be padded * @param appendix string to be appended to origin string * @return padded string * @see #repeat(String, int) */ public static String padEnd(int len, String origin, String appendix) { int originLen = origin.length(); if (originLen >= len) return origin; return origin + repeat(appendix, len - originLen); } /** * Gets the number of strings to be found at origin string. * * @param origin origin string * @param keyword string to be found * @return count of inclusions */ public static int countOf(String origin, String keyword) { int keywordLen = keyword.length(); int count = 0; for (int i = origin.indexOf(keyword); i >= 0; i = origin.indexOf(keyword, i + keywordLen)) { count++; } return count; } /** * Reverses all the characters in a string. * * @param str string to be reversed * @return reversed string * @see StringBuilder#reverse */ public static String reverse(String str) { if (str == null) return null; return new StringBuilder(str).reverse().toString(); } /** * 가장 마지막에 일치하는 문구를 원하는 문구로 대체한다. * *
{@code
     *     replaceLast("ABC%DEF%GHI", "%", "-");    // ABC%DEF-GHI
     *     replaceLast("ABC%DEF%GHI", "%", "\\$");  // ABC%DEF$GHI
     * }
* * @param text text * @param regex regular expression * @param replacement replacement string * @return string replaced with replacement by last replacer */ public static String replaceLast(String text, String regex, String replacement) { return text.replaceFirst("(?s)(.*)" + regex, "$1" + replacement); } /** * 3자리 숫자마다 ,(comma)로 구분한 문자열을 반환한다. * *
{@code
     *     formatComma("");         // 0
     *     formatComma("-100");     // -100
     *     formatComma("100000");   // 100,000
     * }
* * @param amount amount number * @return formatted number with comma */ public static String formatComma(String amount) { return new DecimalFormat("###,###,###,###,###,###,###").format(amount); } /** * 3자리 숫자마다 ,(comma)로 구분한 문자열을 반환한다. * *
{@code
     *     formatComma(0);      // 0
     *     formatComma(-100);   // -100
     *     formatComma(100000); // 100,000
     * }
* * @param amount amount number * @return formatted number with comma */ public static String formatComma(long amount) { return new DecimalFormat("###,###,###,###,###,###,###").format(amount); } /** * Replicates a string as many times as you want. * *
{@code
     *     repeat(null, 2);     // nullnull
     *     repeat("", 5);       // \u0000
     *     repeat("abc", 3);    // abcabcabc
     * }
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
{@code }
{@link String#join(CharSequence, Iterable)} + {@link Collections#nCopies(int, Object)}{@link StringBuilder#append(String)} + {@link StringBuilder#toString()}{@link StringBuffer#append(String)} + {@link StringBuffer#toString()}
1 million1359.5863 ms775.0314 ms888.8059 ms
10 million4232.0463 ms5798.3983 ms6404.1909 ms
1 billion31917.2627 ms60978.4536 ms68058.4082 ms
* * @param str string to be repeated * @param cnt repetition count * @return repeated string */ public static String repeat(String str, int cnt) { return String.join("", Collections.nCopies(cnt, str)); } /** * Replicates a character as many times as you want. * *
{@code
     *     repeat(' ', 3);    // \u0020
     *     repeat('a', 3);    // aaa
     * }
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
{@code }
{@link String#join(CharSequence, Iterable)} + {@link Collections#nCopies(int, Object)}{@link StringBuilder#append(char)} + {@link StringBuilder#toString()}{@link StringBuffer#append(char)} + {@link StringBuffer#toString()}
1 million1359.5863 ms775.0314 ms888.8059 ms
10 million4232.0463 ms5798.3983 ms6404.1909 ms
1 billion31917.2627 ms60978.4536 ms68058.4082 ms
* * @param c character to be repeated * @param cnt repetition count * @return repeated string */ public static String repeat(char c, int cnt) { return String.join("", Collections.nCopies(cnt, String.valueOf(c))); } public static String match(String regex, String src) { return match(regex, src, 0); } public static String match(String regex, String src, int groupNo) { Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(src); String matched = null; while (matcher.find()) { matched = matcher.group(groupNo); } return matched; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy