Class StringUtils

java.lang.Object
org.openhab.core.util.StringUtils

@NonNullByDefault public class StringUtils extends Object
Static utility methods that are helpful when dealing with strings.
Author:
Leo Siepel - Initial contribution
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • abbreviate

      public static @Nullable String abbreviate(@Nullable String input, int maxWidth)
      Input string is shortened to the maxwidth, the last 3 chars are replaced by ... For example: (maxWidth 18) input="openHAB is the greatest ever", return="openHAB is the ..."
      Parameters:
      input - input string
      maxWidth - maxmimum amount of characters to return (including ...)
      Returns:
      Abbreviated String
    • chomp

      public static @Nullable String chomp(@Nullable String str)
      If a newline char exists at the end of the line, it is removed
       Util.chomp(null)          = null
       Util.chomp("")            = ""
       Util.chomp("abc \r")      = "abc "
       Util.chomp("abc\n")       = "abc"
       Util.chomp("abc\r\n")     = "abc"
       Util.chomp("abc\r\n\r\n") = "abc\r\n"
       Util.chomp("abc\n\r")     = "abc\n"
       Util.chomp("abc\n\rabc")  = "abc\n\rabc"
       Util.chomp("\r")          = ""
       Util.chomp("\n")          = ""
       Util.chomp("\r\n")        = ""
       
      Parameters:
      str - the input String to escape, may be null
      Returns:
      the chomped string, may be null
    • escapeXml

      public static @Nullable String escapeXml(@Nullable String str)
      Simple method to escape XML special characters in String. There are five XML special characters which needs to be escaped:
      
       & - &
       < - &lt;
       > - &gt;
       " - &quot;
       ' - &apos;
       
      Parameters:
      str - the input xml as String to escape, may be null
      Returns:
      the escaped xml as String, may be null
    • capitalize

      public static @Nullable String capitalize(@Nullable String str)
      Capitalizes a String changing the first character to title case. No other characters are changed.
       ""       => ""
       "cat"    => "Cat"
       "cAt"    => "CAt"
       "'cat'"  => "'cat'"
       
      Parameters:
      str - the String to capitalize, may be null
      Returns:
      the capitalized String, may be null
    • capitalizeByUnderscore

      public static @Nullable String capitalizeByUnderscore(@Nullable String str)
      Capitalizes words in the string. Only the first char of every word is capitalized, other are set to lowercase. Words are recognized by an underscore delimiter.
       "openHAB_is_cool"   => "Openhab_Is_Cool"
       "foobar_Example" => "Foobar_Example"
       
      Parameters:
      str - the String to capitalize, may be null
      Returns:
      the capitalized String, may be null
    • capitalizeByWhitespace

      public static @Nullable String capitalizeByWhitespace(@Nullable String str)
      Capitalizes words in the string. Only the first char of every word is capitalized, the rest is left as is. Words are recognized by any whitespace.
       "openHAB is cool"   => "OpenHAB Is Cool"
       "foobar Example" => "Foobar Example"
       
      Parameters:
      str - the String to capitalize, may be null
      Returns:
      the capitalized String, may be null
    • padLeft

      public static String padLeft(@Nullable String str, int minSize, String padString)
      Pads the string from the left
       padLeft("9", 4, "0")        => "0009"
       padLeft("3112", 12, "*")    => "********3112"
       padLeft("openHAB", 4, "*")  => "openHAB"
       
      Parameters:
      str - the String to pad, may be null
      minSize - the minimum String size to return
      padString - the String to add when padding
      Returns:
      the padded String
    • padRight

      public static String padRight(@Nullable String str, int minSize, String padString)
      Pads the string from the right
       padRight("9", 4, "0")        => "9000"
       padRight("3112", 12, "*")    => "3112********"
       padRight("openHAB", 4, "*")  => "openHAB"
       
      Parameters:
      str - the String to pad, may be null
      minSize - the minimum String size to return
      padString - the String to add when padding
      Returns:
      the padded String
    • getRandomString

      public static String getRandomString(int length, String charset)
      Creates a random string
      Parameters:
      length - the length of the String to return
      charset - the characters to use to create the String
      Returns:
      the random String
    • getRandomAlphabetic

      public static String getRandomAlphabetic(int length)
      Creates a random string with [A-Zaz] characters
      Parameters:
      length - the length of the String to return
      Returns:
      the random String
    • getRandomHex

      public static String getRandomHex(int length)
      Creates a random string with only hex characters
      Parameters:
      length - the length of the String to return
      Returns:
      the random String
    • getRandomAlphanumeric

      public static String getRandomAlphanumeric(int length)
      Creates a random string with [A-Za-z0-9] characters
      Parameters:
      length - the length of the String to return
      Returns:
      the random String
    • splitByCharacterType

      public static String[] splitByCharacterType(@Nullable String str)
      Splits the string by character type into an array
       "ab de fg"   => "ab", " ", "de", " ", "fg";
       "ab   de fg" => "ab", "   ", "de", " ", "fg";
       "ab:cd:ef"   => "ab", ":", "cd", ":", "ef";
       "number5"    =>  "number", "5"
       "fooBar"     =>  "foo", "Bar"
       "OHRules"    =>  "OH", "Rules"
       
      Parameters:
      str - the input String to split, may be null
      Returns:
      the splitted String
    • unEscapeXml

      public static @Nullable String unEscapeXml(@Nullable String str)
      Simple method to un escape XML special characters in String. There are five XML Special characters which needs to be escaped:
      
       & => &amp;
       < => &lt;
       > => &gt;
       " => &quot;
       ' => &apos;
       
      Parameters:
      str - the input xml as String to unescape, may be null
      Returns:
      the unescaped xml as String, may be null