Class StringUtils

java.lang.Object
org.gbif.utils.text.StringUtils

public final class StringUtils extends Object
Utils class adding specific string methods to existing guava Strings and commons StringUtils.
  • Field Details

  • Method Details

    • foldToAscii

      public static String foldToAscii(String x)
      Removes accents & diacretics and converts ligatures into several chars
      Parameters:
      x - string to fold into ASCII
      Returns:
      string converted to ASCII equivalent, expanding common ligatures
    • thenJoin

      public static String thenJoin(Function<String,String> fct, String... parts)
      Apply a function then join the result using a space if not null. E.g. can be used with apache.commons.lang3.StringUtils::trimToNull to compose a name when some parts are optionals.
      Parameters:
      fct - the function to apply or Function.identity() if none
      parts -
      Returns:
      a String that represents all parts joined by a space or empty String. Never null.
    • increase

      public static String increase(String x)
      Increase a given string by 1, i.e. increase the last char in that string by one. If its a z or Z the char before is increased instead and a new char a is appended. Only true letters are increased, but spaces, punctuation or numbers remain unchanged. Null values stay null and empty strings empty. The case of existing characters will be kept and the appended chars will use the case of the last char of the original string. For example "Carlb" becomes "Carla", "Aua" "Atz", "zZz" "aAaa" or "Abies zzz" "Abiet aaa".
      Parameters:
      x -
      Returns:
    • randomSpecies

      public static String randomSpecies()
      Creates a random species binomial with no meaning at all, but highly randomized.
      Returns:
      a random canonical species name
    • randomGenus

      public static String randomGenus()
    • randomEpithet

      public static String randomEpithet()
    • randomFamily

      public static String randomFamily()
    • randomAuthor

      public static String randomAuthor()
    • randomString

      public static String randomString(int len)
      Creates a random string in upper case of given length with purely latin characters only. Vocals are used much more frequently than consonants
      Parameters:
      len -
      Returns:
      a random string in upper case
    • randomSpeciesYear

      public static String randomSpeciesYear()
      Returns:
      a year since Linnéan times 1751 before now as a 4 character long string
    • parseInteger

      public static Integer parseInteger(String x)
      Simple integer parsing method that does not throw any exception but returns null instead.
      Parameters:
      x -
      Returns:
      the parsed integer or null
    • parseBoolean

      public static Boolean parseBoolean(String x)
      Simple boolean parsing method that understands yes,y,true,t or 1 as true and respective values for false. It does not throw any exception but returns null instead.
      Parameters:
      x -
      Returns:
      the parsed integer or null
    • unescapeUnicodeChars

      public static String unescapeUnicodeChars(String text)
      Unescapes various unicode escapes if existing: java unicode escape, four hexadecimal digits \ uhhhh octal escape \nnn The octal value nnn, where nnn stands for 1 to 3 digits between ‘0’ and ‘7’. For example, the code for the ASCII ESC (escape) character is ‘\033’. hexadecimal escape \xhh... The hexadecimal value hh, where hh stands for a sequence of hexadecimal digits (‘0’–‘9’, and either ‘A’–‘F’ or ‘a’–‘f’).Like the same construct in ISO C, the escape sequence continues until the first nonhexadecimal digit is seen. However, using more than two hexadecimal digits produces undefined results. (The ‘\x’ escape sequence is not allowed in POSIX awk.)
      Parameters:
      text - string potentially containing unicode escape chars
      Returns:
      the unescaped string
    • decodeUtf8Garbage

      public static String decodeUtf8Garbage(String text)
      Tries to decode a UTF8 string only if common UTF8 character combinations are found which are unlikely to be correctly encoded text. E.g. ü is the German Umlaut ü and indicates we have encoded utf8 text still.
    • joinIfNotNull

      public static String joinIfNotNull(String delimiter, Object... values)
      Joins a list of objects into a string, skipping null values and calling toString on each object.
      Parameters:
      delimiter - to join the values with
      values - to be joined
      Returns:
    • upper

      public static Map<String,String> upper(Map<String,String> map)
      Uppercases all keys and values in a Map. If keys clash only one entry will remain, which is not guaranteed.
      Parameters:
      map -
      Returns:
      new map with keys and values upper cased.
    • emptyLowerCase

      public static String emptyLowerCase(String str)
      Returns an empty string or the trimmed lower case version of any input, but never NULL.
    • getStackTrace

      public static String getStackTrace(Throwable aThrowable)
      Reads a stack trace from an exception and returns it as a String.
      Parameters:
      aThrowable -
      Returns:
      teh full stack trace as a String
    • trim

      public static String trim(String str)
      Strips a set of whitespace characters from the start and end of a String. This is similar to String.trim() but also includes some specific characters.
      Parameters:
      str - String to be trimmed
      Returns:
      trimmed String
    • deleteWhitespace

      public static String deleteWhitespace(String str)
      Removes all whitespace characters from the String.
      Parameters:
      str - String to be processed
      Returns:
      String without whitespaces