Package org.openhab.core.util
Class StringUtils
java.lang.Object
org.openhab.core.util.StringUtils
Static utility methods that are helpful when dealing with strings.
- Author:
- Leo Siepel - Initial contribution
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable String
capitalize
(@Nullable String str) Capitalizes a String changing the first character to title case.static @Nullable String
capitalizeByUnderscore
(@Nullable String str) Capitalizes words in the string.static @Nullable String
capitalizeByWhitespace
(@Nullable String str) Capitalizes words in the string.static @Nullable String
If a newline char exists at the end of the line, it is removedstatic @Nullable String
Simple method to escape XML special characters in String.static String
getRandomAlphabetic
(int length) Creates a random string with [A-Zaz] charactersstatic String
getRandomAlphanumeric
(int length) Creates a random string with [A-Za-z0-9] charactersstatic String
getRandomHex
(int length) Creates a random string with only hex charactersstatic String
getRandomString
(int length, String charset) Creates a random stringstatic String
Pads the string from the leftstatic String[]
splitByCharacterType
(@Nullable String str) Splits the string by character type into an arraystatic @Nullable String
unEscapeXml
(@Nullable String str) Simple method to un escape XML special characters in String.
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
chomp
If a newline char exists at the end of the line, it is removedUtil.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
Simple method to escape XML special characters in String. There are five XML special characters which needs to be escaped:& - & < - < > - > " - " ' - '
- Parameters:
str
- the input xml as String to escape, may be null- Returns:
- the escaped xml as String, may be null
-
capitalize
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
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
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
Pads the string from the leftpadLeft("9", 4, "0") => "0009" padLeft("3112", 12, "*") => "********3112" padLeft("openHAB", 4, "*") => "openHAB"
- Parameters:
str
- the String to pad, may be nullminSize
- the minimum String size to returnpadString
- the String to add when padding- Returns:
- the padded String
-
getRandomString
Creates a random string- Parameters:
length
- the length of the String to returncharset
- the characters to use to create the String- Returns:
- the random String
-
getRandomAlphabetic
Creates a random string with [A-Zaz] characters- Parameters:
length
- the length of the String to return- Returns:
- the random String
-
getRandomHex
Creates a random string with only hex characters- Parameters:
length
- the length of the String to return- Returns:
- the random String
-
getRandomAlphanumeric
Creates a random string with [A-Za-z0-9] characters- Parameters:
length
- the length of the String to return- Returns:
- the random String
-
splitByCharacterType
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
Simple method to un escape XML special characters in String. There are five XML Special characters which needs to be escaped:& => & < => < > => > " => " ' => '
- Parameters:
str
- the input xml as String to unescape, may be null- Returns:
- the unescaped xml as String, may be null
-