Class StringUtils

java.lang.Object
org.openl.util.StringUtils

public class StringUtils extends Object
Utils to manipulate with strings.
Author:
Yury Molchan
  • Field Details

  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • split

      public static String[] split(String str, char separator)

      Splits the provided string into an array of trimmed strings, separator specified. The separator is not included in the returned String array. Adjacent separators are treated as one separator.

      A null input String returns null.

       StringUtils.split(null, *)         = null
       StringUtils.split("", *)           = []
       StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
       StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
       StringUtils.split("a . .b.c", '.') = ["a", "b", "c"]
       StringUtils.split(" a b:c ", '.')  = ["a b:c"]
       StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
       StringUtils.split(" a, b, c", ',') = ["a", "b", "c"]
       
      Parameters:
      str - the String to parse, may be null
      separator - the character used as the delimiter
      Returns:
      an array of parsed Strings, null if null String input
    • split

      public static String[] split(String str)

      Splits the provided text into an array, using whitespace as the separator. Whitespace is defined by Character.isWhitespace(char).

      The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

      A null input String returns null.

       StringUtils.split(null)         = null
       StringUtils.split("")           = []
       StringUtils.split("abc def")    = ["abc", "def"]
       StringUtils.split("abc  def")   = ["abc", "def"]
       StringUtils.split(" abc ")      = ["abc"]
       StringUtils.split(" abc def ")  = ["abc", "def"]
       
      Parameters:
      str - the String to parse, may be null
      Returns:
      an array of parsed Strings, null if null String input
    • toLines

      public static String[] toLines(String text)

      Splits the provided string into an array of trimmed strings, separated by new line character codes: '\n' and '\r'. Blank lines are omitted from the result.

      A blank or null input String returns null.

       StringUtils.toLines(null)                 = null
       StringUtils.toLines("")                   = null
       StringUtils.toLines("   ")                = null
       StringUtils.toLines("  abc  ")            = ["abc"]
       StringUtils.toLines("a b c")              = ["a b c"]
       StringUtils.toLines("a\rb\nc")            = ["a", "b", "c"]
       StringUtils.toLines("\r a\n\t\r \n c \n") = ["a", "c"]
       
      Parameters:
      text - the String to parse, may be null
      Returns:
      an array of parsed Strings, null if blank String input
    • join

      public static String join(Object[] values, String separator)

      Joins the elements of the provided array into a single String containing the provided list of elements.

      No delimiter is added before or after the list. A null separator is the same as an empty String (""). Null objects or empty strings within the array are represented by empty strings.

       StringUtils.join(null, *)                = null
       StringUtils.join([], *)                  = ""
       StringUtils.join([null], *)              = "null"
       StringUtils.join(["a", "b", "c"], "--")  = "a--b--c"
       StringUtils.join(["a", "b", "c"], "")    = "abc"
       StringUtils.join([null, "", "a"], ',')   = "null,,a"
       
      Parameters:
      values - the array of values to join together, may be null
      separator - the separator character to use, null treated as ""
      Returns:
      the joined String, null if null array input
    • isEmpty

      public static boolean isEmpty(CharSequence cs)

      Checks if a CharSequence is empty ("") or null.

       StringUtils.isEmpty(null)      = true
       StringUtils.isEmpty("")        = true
       StringUtils.isEmpty(" ")       = false
       StringUtils.isEmpty("boo")     = false
       StringUtils.isEmpty("  boo  ") = false
       
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is empty or null
    • isNotEmpty

      public static boolean isNotEmpty(CharSequence cs)

      Checks if a CharSequence is not empty ("") and not null.

       StringUtils.isNotEmpty(null)      = false
       StringUtils.isNotEmpty("")        = false
       StringUtils.isNotEmpty(" ")       = true
       StringUtils.isNotEmpty("boo")     = true
       StringUtils.isNotEmpty("  boo  ") = true
       
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is not empty and not null
    • isBlank

      public static boolean isBlank(CharSequence cs)

      Checks if a CharSequence is whitespace, empty ("") or null.

       StringUtils.isBlank(null)      = true
       StringUtils.isBlank("")        = true
       StringUtils.isBlank(" ")       = true
       StringUtils.isBlank("boo")     = false
       StringUtils.isBlank("  boo  ") = false
       
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is null, empty or whitespace
    • isNotBlank

      public static boolean isNotBlank(CharSequence cs)

      Checks if a CharSequence is not empty (""), not null and not whitespace only.

       StringUtils.isNotBlank(null)      = false
       StringUtils.isNotBlank("")        = false
       StringUtils.isNotBlank(" ")       = false
       StringUtils.isNotBlank("boo")     = true
       StringUtils.isNotBlank("  boo  ") = true
       
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is not empty and not null and not whitespace
    • containsIgnoreCase

      public static boolean containsIgnoreCase(String str, String searchStr)

      Checks if String contains a search String irrespective of case, handling null. Case-insensitivity is defined as by String.equalsIgnoreCase(String).

      A null String will return false.

       StringUtils.contains(null, *)     = false
       StringUtils.contains(*, null)     = false
       StringUtils.contains("", "")      = true
       StringUtils.contains("abc", "")   = true
       StringUtils.contains("abc", "a")  = true
       StringUtils.contains("abc", "Bc") = true
       StringUtils.contains("abc", "z")  = false
       StringUtils.contains("abc", "Z")  = false
       
      Parameters:
      str - the String to check, may be null
      searchStr - the String to find, may be null
      Returns:
      true if the String contains the search String irrespective of case or false if not or null string input
    • matches

      public static boolean matches(Pattern regex, CharSequence input)
      Do the same as String.matches(String)
      Parameters:
      regex - a Pattern to which this string is to be matched
      input - an input string to match regexp Pattern
      Returns:
      true if, and only if, this string matches the given regular expression
    • trim

      public static String trim(String str)

      Removes control characters (char <= 32 or (>= 127 and <= 160) from both ends of this String, handling null by returning null.

       StringUtils.trim(null)          = null
       StringUtils.trim("")            = ""
       StringUtils.trim("     ")       = ""
       StringUtils.trim("boo")         = "boo"
       StringUtils.trim("    boo    ") = "boo"
       
      Parameters:
      str - the String to be trimmed, may be null
      Returns:
      the trimmed string, null if null String input
    • isSpaceOrControl

      public static boolean isSpaceOrControl(int ch)
      Determines if the specified character is whitespace or control character (char <= 32 or (>= 127 and <= 160)
      Parameters:
      ch - the character to be tested
      Returns:
      true if character is whitespace or control, otherwise false
    • trimToNull

      public static String trimToNull(String str)

      Removes control characters (char <= 32 or (>= 127 and <= 160) from both ends of this String returning null if the String is empty ("") after the trim or if it is null.

       StringUtils.trimToNull(null)          = null
       StringUtils.trimToNull("")            = null
       StringUtils.trimToNull("     ")       = null
       StringUtils.trimToNull("boo")         = "boo"
       StringUtils.trimToNull("    boo    ") = "boo"
       
      Parameters:
      str - the String to be trimmed, may be null
      Returns:
      the trimmed String, null if only chars <= 32, empty or null String input
    • trimToEmpty

      public static String trimToEmpty(String str)

      Removes control characters (char <= 32 or (>= 127 and <= 160) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is null.

       StringUtils.trimToEmpty(null)          = ""
       StringUtils.trimToEmpty("")            = ""
       StringUtils.trimToEmpty("     ")       = ""
       StringUtils.trimToEmpty("boo")         = "boo"
       StringUtils.trimToEmpty("    boo    ") = "boo"
       
      Parameters:
      str - the String to be trimmed, may be null
      Returns:
      the trimmed String, or an empty String if null input
    • capitalize

      public static String capitalize(String str)

      Capitalizes a String changing the first letter to title case as per Character.toTitleCase(char). No other letters are changed.

      A null input String returns null.

       StringUtils.capitalize(null)  = null
       StringUtils.capitalize("")    = ""
       StringUtils.capitalize("foo") = "Foo"
       StringUtils.capitalize("fOo") = "FOo"
       
      Parameters:
      str - the String to capitalize, may be null
      Returns:
      the capitalized String, null if null String input
      See Also:
    • uncapitalize

      public static String uncapitalize(String str)

      Uncapitalizes a String changing the first letter to title case as per Character.toLowerCase(char). No other letters are changed.

      A null input String returns null.

       StringUtils.uncapitalize(null)  = null
       StringUtils.uncapitalize("")    = ""
       StringUtils.uncapitalize("Foo") = "foo"
       StringUtils.uncapitalize("FOO") = "fOO"
       
      Parameters:
      str - the String to uncapitalize, may be null
      Returns:
      the uncapitalized String, null if null String input
      See Also:
    • camelToKebab

      public static String camelToKebab(String camel)
      Converts CamelCased string to kebab-cased. It is useful for converting Java's fields or methods to case insensetive form, e.g. for properties keys, urls, MS Windows file names and e.t.c.
       StringUtils.camelToKebab(null)        = null
       StringUtils.camelToKebab("")          = ""
       StringUtils.camelToKebab("FOO")       = "foo"
       StringUtils.camelToKebab("Foo")       = "foo"
       StringUtils.camelToKebab("foo")       = "foo"
       StringUtils.camelToKebab("FooBar")    = "foo-bar"
       StringUtils.camelToKebab("fooBar")    = "foo-bar"
       StringUtils.camelToKebab("FOOBar")    = "foo-bar"
       StringUtils.camelToKebab("ABar")      = "a-bar"
       StringUtils.camelToKebab("aBar")      = "a-bar"
       StringUtils.camelToKebab("aBAR")      = "a-bar"
       
      Parameters:
      camel - - CamelCased string
      Returns:
      - kebab-cased string
    • first

      public static int first(CharSequence text, int start, int end, IntPredicate tester)
      Find position of the first occurrence of the symbol is matched to the predicate.
      Parameters:
      text - the string where a not white space symbol should be searched
      start - the start position of the searching inclusive
      end - the final position of searching exclusive
      tester - the predicate for searching the symbol
      Returns:
      position of the symbol or -1
    • last

      public static int last(CharSequence text, int start, int end, IntPredicate tester)
      Find position of the last occurrence of the symbol is matched to the predicate.
      Parameters:
      text - the string where a not white space symbol should be searched
      start - the start position of the searching inclusive
      end - the final position of searching exclusive
      tester - the predicate for searching the symbol
      Returns:
      position of the symbol or -1
    • firstNonSpace

      public static int firstNonSpace(CharSequence text, int start, int end)
      Find position of the next not white space symbol.
      Parameters:
      text - the string where a not white space symbol should be searched
      start - the start position of the searching inclusive
      end - the final position of searching exclusive
      Returns:
      position of a not white space symbol or -1
    • lastNonSpace

      public static int lastNonSpace(CharSequence text, int start, int end)
      Find position of the last not white space symbol.
      Parameters:
      text - the string where a not white space symbol should be searched
      start - the start position of the searching inclusive
      end - the final position of searching exclusive
      Returns:
      position of a not white space symbol or -1