Class StringUtil

java.lang.Object
com.tccc.kos.commons.util.misc.StringUtil

public final class StringUtil extends Object
Utility class that handles various string-based operations. Many are similar to Apache commons-lang3 StringUtils methods, however we don't want to include that large library for just a few methods.
Since:
1.0
Version:
2023-08-25
  • Constructor Details

    • StringUtil

      public StringUtil()
  • Method Details

    • length

      public static int length(CharSequence charSequence)
      Gets a CharSequence length or 0 if the CharSequence is null .
      Parameters:
      charSequence - a CharSequence or null
      Returns:
      CharSequence length or 0 if the CharSequence is null .
    • hasValue

      public static boolean hasValue(CharSequence charSequence)
      Determines if the given CharSequence has a value or not.
      Parameters:
      charSequence - the CharSequence to check, may be null
      Returns:
      true if the charSequence has any non-whitespace characters
    • isEmpty

      public static boolean isEmpty(CharSequence charSequence)
      Checks if a CharSequence is empty ("") or null. Taken from Apache commons-lang3 StringUtils method of the same name.
       Str.isEmpty(null)      = true Str.isEmpty("")        = true Str.isEmpty(" ")       = false Str.isEmpty("bob")     = false Str.isEmpty("  bob  ") = false 
      Parameters:
      charSequence - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is empty or null
    • isBlank

      public static boolean isBlank(CharSequence charSequence)
      Checks if a CharSequence is empty (""), null or whitespace only. Whitespace is defined by Character.isWhitespace(char) .
       Str.isBlank(null)      = true Str.isBlank("")        = true Str.isBlank(" ")       = true Str.isBlank(" \t\r\n") = true Str.isBlank("bob")     = false Str.isBlank("  bob  ") = false 
      Parameters:
      charSequence - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is null, empty or whitespace only
    • notNull

      public static String notNull(String str)
      Ensures that the given string is not null.
      Parameters:
      str - the input string
      Returns:
      the input string if it's not null, otherwise the empty string
    • notBlank

      public static String notBlank(String str)
      Ensures that the given string is not blank.
      Parameters:
      str - the input string
      Returns:
      the input string if it's not blank, otherwise null
    • trim

      public static String trim(String str)
      Trims the given string, or returns null if input is null.
      Parameters:
      str - the input string
      Returns:
      the trimmed string
    • appendIfMissing

      public static String appendIfMissing(String str, String suffix)
      Appends the suffix to the end of the string if the string does not already end with it. Based on the Apache commons-lang3 StringUtils method of the same name.
       Str.appendIfMissing(null, null)      = null Str.appendIfMissing("abc", null)     = "abc" Str.appendIfMissing("", "xyz")       = "xyz" Str.appendIfMissing("abc", "xyz")    = "abcxyz" Str.appendIfMissing("abcxyz", "xyz") = "abcxyz" Str.appendIfMissing("abcXYZ", "xyz") = "abcXYZxyz" 
      Parameters:
      str - the input string
      suffix - the suffix to check for and append to the end of the string
      Returns:
      a new String if suffix was appended, the same string otherwise.
    • calcParentDir

      public static String calcParentDir(String fileOrDirPath)
      Given a file or directory path, return its parent directory.
       Str.calcParentDir(null)                 = null Str.calcParentDir("/")                  = null Str.calcParentDir("/index.html")        = "/" Str.calcParentDir("/foo/")              = "/" Str.calcParentDir("/foo/bar/")          = "/foo/" Str.calcParentDir("/foo/bar/image.jpg") = "/foo/bar/" 
      Parameters:
      fileOrDirPath - complete file or directory name
      Returns:
      parent directory, or null if top-level passed in
    • fixFileSeparators

      public static String fixFileSeparators(String inputPath)
      Replaces all "\" characters with "/" in the given string. Related to the operating system's "file separator", which is retrieved by FileSystems.getDefault().getSeparator() .
      Parameters:
      inputPath - input file or directory path
      Returns:
      converted string
    • split

      public static String[] split(String input, String separator)
      Null-safe way to split a string into its individual components. Unlike the default String.split() method, trailing empty strings ARE included in the resulting array.
       Str.split(null,    ",") = [] Str.split("1,2,3", ",") = ["1", "2", "3"] Str.split("1:2:3", ":") = ["1", "2", "3"] Str.split(",2,3",  ",") = ["",  "2", "3"]  : missing first string Str.split("1,,3",  ",") = ["1", "",  "3"]  : missing middle string Str.split("1,2,",  ",") = ["1", "2", "" ]  : missing trailing string 
      Parameters:
      input - input text
      separator - separator string (not a regular expression!)
    • isJson

      public static boolean isJson(String input)
      Return true if the string starts with { or [ which indicates that the string is a json payload.
      Parameters:
      input - the string to check
    • equals

      public static boolean equals(String str1, String str2)
      Return true if the specified strings are equal. Handles comparing nulls.
      Parameters:
      str1 - input string
      str2 - input string
    • getClassName

      public static String getClassName(Class<?> clazz)
      Returns the simple class name, but includes enclosing class if nested. For example, "com.tccc.kos...Trouble$View" returns "Trouble$View". This is different from clazz.getSimpleName(), which simply returns "View".
    • getClassName

      public static String getClassName(String fullClassName)
      Same as above, except the class name is passed in.
    • getNotNull

      public static String getNotNull(String str1, String str2)
      Given two strings, return the first if not null, otherwise return the second.