Klasse StringUtil

java.lang.Object
com.github.hypfvieh.util.StringUtil

public final class StringUtil extends Object
Utility class for String manipulation.
Seit:
v1.0 - 2016-06-29
Autor:
hypfvieh
  • Methodenübersicht

    Modifizierer und Typ
    Methode
    Beschreibung
    static String
    abbreviate(String _str, int _length)
    Abbreviates a String using ellipses.
    static String
    concatStrings(boolean _ignoreEmpty, String _separator, String... _parts)
    Concats all strings using the separator as delimiter.
    static boolean
    containsAny(boolean _ignoreCase, String _str, String... _args)
    Checks if any of the given strings in _args is contained in _str.
    static boolean
    containsAny(String _str, String... _args)
    Checks if any of the given strings in _args is contained in _str, case-sensitive.
    static boolean
    Checks if any of the given strings in _args is contained in _str, case-insensitve.
    static String
    Converts a camel-case string to an upper-case string where each upper-case character except the first in the input string is preceded by an underscore in the output string.
    static String
    Tries to convert upper-case string to camel-case.
    static int
    countSubString(String string, String substring)
    Count the number of instances of substring within a string.
    static String
    defaultIfBlank(String _str, String _default)
    Checks if given String is blank (see isBlank(String).
    If String is blank, the given default is returned, otherwise the String is returned.
    static boolean
    endsWithAny(boolean _ignoreCase, String _str, String... _args)
    Checks if given string in _str ends with any of the given strings in _args.
    static boolean
    equalsAny(boolean _ignoreCase, String _str, String... _compare)
    Checks if any of the given strings in _compare is equal to _str (either case-insensitive or case-sensitive).
    Will return true if both parameters are null or _str is null and _compare is empty.
    static boolean
    equalsAny(String _str, String... _compare)
    Checks if any of the given strings in _compare is equal to _str (case-sensitive).
    Will return true if both parameters are null or _str is null and _compare is empty.
    static boolean
    equalsIgnoreCaseAny(String _str, String... _compare)
    Checks if any of the given strings in _compare is equal to _str (case-insensitive).
    Will return true if both parameters are null or _str is null and _compare is empty.
    static boolean
    Checks if the given String is in all upper-case.
    static boolean
    Checks if the given String is either null or blank.
    static boolean
    Checks if the given String is either null or empty.
    static boolean
    Checks if the given String is NOT blank.
    static String
    join(String _delimiter, String[] _strings)
    Combines the Strings in _string using _delimiter.
    static String
    join(String _delimiter, List<String> _strings)
    Combines the Strings in _string using _delimiter.
    static String
    Lower case the first letter of the given string.
    static String
    mask(String _str, String _maskStr, int _maskBgn, int _maskRpt)
    Mask the given string with the given pattern starting at given start and ending on given end of string.
    static String
    randomString(int _length)
    Generate a simple (cryptographic insecure) random string.
    static String
    repeat(String _str, int _count)
    Repeats the given string pattern for the given times.
    static String
    replaceByMap(String _searchStr, Map<String,String> _replacements)
    Replace all placeholders in given string by value of the corresponding key in given Map.
    static <T extends CharSequence>
    T
     
    static <T extends CharSequence>
    T
    requireNonBlank(T _str, String _message)
    Checks that the specified string is not blank.
    static <T extends CharSequence>
    T
     
    static <T extends CharSequence>
    T
    requireNonEmpty(T _str, String _message)
    Checks that the specified string is not empty.
    static String
    rightPad(String _str, int _size, String _padChar)
    Right-pads a string with spaces (' ').
    The String is padded to the size of _size.
    static String
    rot13(String _input)
    Simple rot13 implementation.
    static List<String>
    smartWordSplit(String _text, int _len)
    Tries to split a string in a smart way.

    String will be splitted by space and then recombined until each line has the given length or less than the given length, if the next token would cause the line to be longer than requested It is ensured that each line has a maximum length of _len, it could be short but never longer.
    static String
    Converts a snake-case-string to camel case string.
    static String[]
    split(String _str)
    Split string by whitespace.
    static String[]
    split(String _str, char _separatorChar)
    Split a String by the given delimiting char.
    static String[]
    split(String _str, String _separatorStr)
    Split a String by the given delimiting char.
    static List<String>
    splitEqually(String _text, int _len)
    Splits a Text to equal parts.
    static List<String>
    splitToList(String _str, char _separatorChar)
    Split given String using given delimiting char.
    static List<String>
    splitToList(String _str, String _separatorStr)
    Split given String using the given delimiting string.
    static boolean
    startsWithAny(boolean _ignoreCase, String _str, String... _args)
    Checks if given string in _str starts with any of the given strings in _args.
    static String
    trim(String _str)
    Removes trailing and leading spaces / non-printable characters (char <= 32).
    static String
    Upper case the first letter of the given string.

    Von Klasse geerbte Methoden java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Methodendetails

    • abbreviate

      public static String abbreviate(String _str, int _length)
      Abbreviates a String using ellipses.
      Parameter:
      _str - string to abbrivate
      _length - max length
      Gibt zurück:
      abbreviated string, original string if string length is lower or equal then desired length or null if input was null
    • smartWordSplit

      public static List<String> smartWordSplit(String _text, int _len)
      Tries to split a string in a smart way.

      String will be splitted by space and then recombined until each line has the given length or less than the given length, if the next token would cause the line to be longer than requested It is ensured that each line has a maximum length of _len, it could be short but never longer.
      Parameter:
      _text - text to split
      _len - max length per line
      Gibt zurück:
      list or null if _text was null
    • splitEqually

      public static List<String> splitEqually(String _text, int _len)
      Splits a Text to equal parts. There is no detection of words, everything will be cut to the same length.
      Parameter:
      _text - text to split
      _len - max length per line
      Gibt zurück:
      list of string splitted to _len or null if _text was null
    • replaceByMap

      public static String replaceByMap(String _searchStr, Map<String,String> _replacements)
      Replace all placeholders in given string by value of the corresponding key in given Map.
      Parameter:
      _searchStr - search string
      _replacements - replacement
      Gibt zurück:
      String or null if _searchStr was null
    • lowerCaseFirstChar

      public static String lowerCaseFirstChar(String _str)
      Lower case the first letter of the given string.
      Parameter:
      _str - string
      Gibt zurück:
      lowercased string
    • upperCaseFirstChar

      public static String upperCaseFirstChar(String _str)
      Upper case the first letter of the given string.
      Parameter:
      _str - string
      Gibt zurück:
      uppercased string
    • rot13

      public static String rot13(String _input)
      Simple rot13 implementation.
      Parameter:
      _input - input to scramble
      Gibt zurück:
      scrambled input (null if input was null)
    • equalsIgnoreCaseAny

      public static boolean equalsIgnoreCaseAny(String _str, String... _compare)
      Checks if any of the given strings in _compare is equal to _str (case-insensitive).
      Will return true if both parameters are null or _str is null and _compare is empty.
      Parameter:
      _str - string to check
      _compare - compare strings
      Gibt zurück:
      true if equal false otherwise
    • equalsAny

      public static boolean equalsAny(String _str, String... _compare)
      Checks if any of the given strings in _compare is equal to _str (case-sensitive).
      Will return true if both parameters are null or _str is null and _compare is empty.
      Parameter:
      _str - string to check
      _compare - compare strings
      Gibt zurück:
      true if equal false otherwise
    • equalsAny

      public static boolean equalsAny(boolean _ignoreCase, String _str, String... _compare)
      Checks if any of the given strings in _compare is equal to _str (either case-insensitive or case-sensitive).
      Will return true if both parameters are null or _str is null and _compare is empty.
      Parameter:
      _ignoreCase - ignore case
      _str - string to check
      _compare - compare strings
      Gibt zurück:
      true if equal false otherwise
    • isBlank

      public static boolean isBlank(CharSequence _str)
      Checks if the given String is either null or blank. Blank means:
       " " - true
       "" - true
       null - true
       " xx" - false
       
      Parameter:
      _str - string to test
      Gibt zurück:
      true if string is blank or null, false otherwise
    • isNotBlank

      public static boolean isNotBlank(CharSequence _str)
      Checks if the given String is NOT blank.
      Parameter:
      _str - string to check
      Gibt zurück:
      true if string is not blank, false otherwise
    • isEmpty

      public static boolean isEmpty(CharSequence _str)
      Checks if the given String is either null or empty. Blank means:
       " " - false
       "" - true
       null - true
       " xx" - false
       
      Parameter:
      _str - string to test
      Gibt zurück:
      true if string is empty or null, false otherwise
    • defaultIfBlank

      public static String defaultIfBlank(String _str, String _default)
      Checks if given String is blank (see isBlank(String).
      If String is blank, the given default is returned, otherwise the String is returned.
      Parameter:
      _str - string to check
      _default - default in case of blank string
      Gibt zurück:
      _str or _default
    • randomString

      public static String randomString(int _length)
      Generate a simple (cryptographic insecure) random string.
      Parameter:
      _length - length of random string
      Gibt zurück:
      random string or empty string if _length <= 0
    • trim

      public static String trim(String _str)
      Removes trailing and leading spaces / non-printable characters (char <= 32).
      Parameter:
      _str - String to trim
      Gibt zurück:
      trimmed string or null if null input
    • join

      public static String join(String _delimiter, List<String> _strings)
      Combines the Strings in _string using _delimiter.
      Parameter:
      _delimiter - delimiting string
      _strings - strings to join
      Gibt zurück:
      null if _strings is null, concatenated string otherwise
    • join

      public static String join(String _delimiter, String[] _strings)
      Combines the Strings in _string using _delimiter.
      Parameter:
      _delimiter - delimiting string
      _strings - string to join
      Gibt zurück:
      null if _strings is null, concatenated string otherwise
    • convertCamelToUpperCase

      public static String convertCamelToUpperCase(String _str)
      Converts a camel-case string to an upper-case string where each upper-case character except the first in the input string is preceded by an underscore in the output string. Empty or null strings are returned as-is.
         convertCamelToUpperCase(null) = null
         convertCamelToUpperCase("") = ""
         convertCamelToUpperCase("  ") = "  "
         convertCamelToUpperCase("Hello") = "HELLO"
         convertCamelToUpperCase("HELLO") = "HELLO"
         convertCamelToUpperCase("AcmeCompany") = "ACME_COMPANY"
       
      Parameter:
      _str - camel-case string
      Gibt zurück:
      upper-case string
    • convertUpperToCamelCase

      public static String convertUpperToCamelCase(String _str)
      Tries to convert upper-case string to camel-case. The given string will be analyzed and all string parts preceded by an underline character will be converted to upper-case, all other following characters to lower-case.
      Parameter:
      _str - string to convert
      Gibt zurück:
      converted string or original string if there was nothing to do
    • isAllUpperCase

      public static boolean isAllUpperCase(String _str)
      Checks if the given String is in all upper-case.
      Parameter:
      _str - string to check
      Gibt zurück:
      true if upper-case, false otherwise. Also false if string is null or empty.
    • containsAnyIgnoreCase

      public static boolean containsAnyIgnoreCase(String _str, String... _args)
      Checks if any of the given strings in _args is contained in _str, case-insensitve.
      Parameter:
      _str - string to check
      _args - patterns to find
      Gibt zurück:
      true if any string in _args is found in _str, false if not or _str/_args is null
    • containsAny

      public static boolean containsAny(String _str, String... _args)
      Checks if any of the given strings in _args is contained in _str, case-sensitive.
      Parameter:
      _str - string to check
      _args - patterns to find
      Gibt zurück:
      true if any string in _args is found in _str, false if not or _str/_args is null
    • containsAny

      public static boolean containsAny(boolean _ignoreCase, String _str, String... _args)
      Checks if any of the given strings in _args is contained in _str.
      Parameter:
      _ignoreCase - true to ignore case, false to be case sensitive
      _str - string to check
      _args - patterns to find
      Gibt zurück:
      true if any string in _args is found in _str, false if not or _str/_args is null
    • endsWithAny

      public static boolean endsWithAny(boolean _ignoreCase, String _str, String... _args)
      Checks if given string in _str ends with any of the given strings in _args.
      Parameter:
      _ignoreCase - true to ignore case, false to be case sensitive
      _str - string to check
      _args - patterns to find
      Gibt zurück:
      true if given string in _str ends with any of the given strings in _args, false if not or _str/_args is null
    • startsWithAny

      public static boolean startsWithAny(boolean _ignoreCase, String _str, String... _args)
      Checks if given string in _str starts with any of the given strings in _args.
      Parameter:
      _ignoreCase - true to ignore case, false to be case sensitive
      _str - string to check
      _args - patterns to find
      Gibt zurück:
      true if given string in _str starts with any of the given strings in _args, false if not or _str/_args is null
    • repeat

      public static String repeat(String _str, int _count)
      Repeats the given string pattern for the given times.
      Parameter:
      _str - string to repeat
      _count - number of repetitions
      Gibt zurück:
      repeated string or null if pattern was null or count was <= 0
    • rightPad

      public static String rightPad(String _str, int _size, String _padChar)
      Right-pads a string with spaces (' ').
      The String is padded to the size of _size.
       StringUtil.rightPad(null, *)   = null
       StringUtil.rightPad("", 3)     = "   "
       StringUtil.rightPad("bat", 3)  = "bat"
       StringUtil.rightPad("bat", 5)  = "bat  "
       StringUtil.rightPad("bat", 1)  = "bat"
       StringUtil.rightPad("bat", -1) = "bat"
       
      Parameter:
      _str - the String to pad out, may be null
      _size - the size to pad to
      _padChar - the padding character
      Gibt zurück:
      right-padded String or original String if no padding is necessary
    • split

      public static String[] split(String _str, char _separatorChar)
      Split a String by the given delimiting char.
      Parameter:
      _str - string to split
      _separatorChar - delimiting char to use
      Gibt zurück:
      null if _str is null, array (maybe empty) otherwise
    • split

      public static String[] split(String _str, String _separatorStr)
      Split a String by the given delimiting char.
      Parameter:
      _str - string to split
      _separatorStr - delimiting String to use
      Gibt zurück:
      null if _str is null, array (maybe empty) otherwise
    • split

      public static String[] split(String _str)
      Split string by whitespace.
      Parameter:
      _str - string to split
      Gibt zurück:
      null if _str is null, array (maybe empty) otherwise
    • splitToList

      public static List<String> splitToList(String _str, char _separatorChar)
      Split given String using given delimiting char.
      Parameter:
      _str - string to split
      _separatorChar - char to use as delimiter
      Gibt zurück:
      list of String tokens, null if _str is null or empty list
    • splitToList

      public static List<String> splitToList(String _str, String _separatorStr)
      Split given String using the given delimiting string.
      Parameter:
      _str - string to split
      _separatorStr - string to us as delimiter
      Gibt zurück:
      list of String tokens, null if _str is null or empty list
    • mask

      public static String mask(String _str, String _maskStr, int _maskBgn, int _maskRpt)
      Mask the given string with the given pattern starting at given start and ending on given end of string.
      If _str is null or _maskStr is null or empty, null is returned.

      If _maskBgn is lower than 0 or _maskLength is lower than 0 or _maskRpt minus _maskBgn is lower than 0, null is returned.

      If _maskBgn is bigger than the length of _str, the original String is returned.
      If _maskRpt is bigger than the length of _str, length of _str is used.
      Parameter:
      _str - string to mask
      _maskStr - mask to use
      _maskBgn - offset to start at (0 based, inclusive)
      _maskRpt - repetitions of _maskStr
      Gibt zurück:
      masked String or null
    • snakeToCamelCase

      public static String snakeToCamelCase(String _input)
      Converts a snake-case-string to camel case string.
      Eg. this_is_snake_case → thisIsSnakeCase
      Parameter:
      _input - string
      Gibt zurück:
      camel case string or input if nothing todo. Returns null if input was null.
    • concatStrings

      public static String concatStrings(boolean _ignoreEmpty, String _separator, String... _parts)
      Concats all strings using the separator as delimiter. Will exclude all null values and optionally ignore empty values.
      Parameter:
      _ignoreEmpty - true to ignore empty strings
      _separator - separator to add between each string
      _parts - parts to concat
      Gibt zurück:
      concatinated string, null if input is null
    • requireNonBlank

      public static <T extends CharSequence> T requireNonBlank(T _str, String _message)
      Checks that the specified string is not blank. This method is designed primarily for doing parameter validation in methods and constructors.
      Parameter:
      _str - the sequence to check for blankness
      _message - exception message if check fails
      Gibt zurück:
      _str if not blank
      Löst aus:
      IllegalArgumentException - if _str is blank
    • requireNonBlank

      public static <T extends CharSequence> T requireNonBlank(T _str)
    • requireNonEmpty

      public static <T extends CharSequence> T requireNonEmpty(T _str, String _message)
      Checks that the specified string is not empty. This method is designed primarily for doing parameter validation in methods and constructors.
      Parameter:
      _str - the sequence to check for emptiness
      _message - exception message if check fails
      Gibt zurück:
      _str if not empty
      Löst aus:
      IllegalArgumentException - if _str is empty
    • requireNonEmpty

      public static <T extends CharSequence> T requireNonEmpty(T _str)
    • countSubString

      public static int countSubString(String string, String substring)
      Count the number of instances of substring within a string.
      Parameter:
      string - String to look for substring in.
      substring - Sub-string to look for.
      Gibt zurück:
      Count of substrings in string.