Package org.gbif.utils.text
Class StringUtils
java.lang.Object
org.gbif.utils.text.StringUtils
Utils class adding specific string methods to existing guava Strings and
commons
StringUtils.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final StringThis includes some special whitespaces which not present in standard trim list: U+0085 Next Line (NEL) U+00A0 No-Break Space (NBSP) U+000C Form Feed (FF) U+2007 Figure Space -
Method Summary
Modifier and TypeMethodDescriptionstatic StringdecodeUtf8Garbage(String text) Tries to decode a UTF8 string only if common UTF8 character combinations are found which are unlikely to be correctly encoded text.static StringdeleteWhitespace(String str) Removes all whitespace characters from the String.static StringemptyLowerCase(String str) Returns an empty string or the trimmed lower case version of any input, but never NULL.static StringRemoves accents & diacretics and converts ligatures into several charsstatic StringgetStackTrace(Throwable aThrowable) Reads a stack trace from an exception and returns it as a String.static StringIncrease a given string by 1, i.e.static StringjoinIfNotNull(String delimiter, Object... values) Joins a list of objects into a string, skipping null values and calling toString on each object.static BooleanSimple boolean parsing method that understands yes,y,true,t or 1 as true and respective values for false.static IntegerSimple integer parsing method that does not throw any exception but returns null instead.static Stringstatic Stringstatic Stringstatic Stringstatic StringCreates a random species binomial with no meaning at all, but highly randomized.static Stringstatic StringrandomString(int len) Creates a random string in upper case of given length with purely latin characters only.static StringApply a function then join the result using a space if not null.static StringStrips a set of whitespace characters from the start and end of a String.static StringunescapeUnicodeChars(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’.Uppercases all keys and values in a Map.
-
Field Details
-
LINNEAN_YEAR
- See Also:
-
WHITESPACES_LIST
This includes some special whitespaces which not present in standard trim list:- U+0085 Next Line (NEL)
- U+00A0 No-Break Space (NBSP)
- U+000C Form Feed (FF)
- U+2007 Figure Space
- See Also:
-
-
Method Details
-
foldToAscii
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
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 noneparts-- Returns:
- a String that represents all parts joined by a space or empty String. Never null.
-
increase
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
Creates a random species binomial with no meaning at all, but highly randomized.- Returns:
- a random canonical species name
-
randomGenus
-
randomEpithet
-
randomFamily
-
randomAuthor
-
randomString
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
- Returns:
- a year since Linnéan times 1751 before now as a 4 character long string
-
parseInteger
Simple integer parsing method that does not throw any exception but returns null instead.- Parameters:
x-- Returns:
- the parsed integer or null
-
parseBoolean
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
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
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
Joins a list of objects into a string, skipping null values and calling toString on each object.- Parameters:
delimiter- to join the values withvalues- to be joined- Returns:
-
upper
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
Returns an empty string or the trimmed lower case version of any input, but never NULL. -
getStackTrace
Reads a stack trace from an exception and returns it as a String.- Parameters:
aThrowable-- Returns:
- teh full stack trace as a String
-
trim
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
Removes all whitespace characters from the String.- Parameters:
str- String to be processed- Returns:
- String without whitespaces
-