Class StringUtils
- java.lang.Object
-
- tech.tablesaw.util.StringUtils
-
public class StringUtils extends Object
Operations onStringthat arenullsafe.StringUtilshandlesnullinput Strings quietly. That is to say that anullinput will returnnull. Where abooleanorintis being returned details vary by method.A side effect of the
nullhandling is that aNullPointerExceptionshould be considered a bug inStringUtils.Methods in this class give sample code to explain their operation. The symbol
*is used to indicate any input includingnull.#ThreadSafe#
- Since:
- 1.0
- See Also:
String
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Stringabbreviate(String str, String abbrevMarker, int maxWidth)Abbreviates a String using ellipses.static Stringcapitalize(String str)Capitalizes a String changing the first character to title case as perCharacter.toTitleCase(int).static booleanisAllLowerCase(String cs)Checks if the CharSequence contains only lowercase characters.static booleanisAllUpperCase(String cs)Checks if the CharSequence contains only uppercase characters.static booleanisAlpha(String cs)Checks if the CharSequence contains only Unicode letters.static booleanisAlphanumeric(String cs)Checks if the CharSequence contains only Unicode letters or digits.static booleanisNumeric(String cs)Checks if the CharSequence contains only Unicode digits.static Stringjoin(Object[] array, char separator)Joins the elements of the provided array into a single String containing the provided list of elements.static intlength(CharSequence cs)Gets a CharSequence length or0if the CharSequence isnull.static StringremoveZeroDecimal(String str)Removes all trailing zero decimals from the given String, assuming all decimals are zero and any zero decimals actually exist.static Stringrepeat(String str, int repeat)Repeat a Stringrepeattimes to form a new String.static String[]splitByCharacterTypeCamelCase(String str)Splits a String by Character type as returned byjava.lang.Character.getType(char).
-
-
-
Method Detail
-
splitByCharacterTypeCamelCase
public static String[] splitByCharacterTypeCamelCase(String str)
Splits a String by Character type as returned byjava.lang.Character.getType(char). Groups of contiguous characters of the same type are returned as complete tokens, with the following exception: ifcamelCaseistrue, the character of typeCharacter.UPPERCASE_LETTER, if any, immediately preceding a token of typeCharacter.LOWERCASE_LETTERwill belong to the following token rather than to the preceding, if any,Character.UPPERCASE_LETTERtoken.- Parameters:
str- the String to split, may benull- Returns:
- an array of parsed Strings,
nullif null String input - Since:
- 2.4
-
join
public static String join(Object[] array, char separator)
Joins the elements of the provided array into a single String containing the provided list of elements.No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.
StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], ';') = "a;b;c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join([null, "", "a"], ';') = ";;a"
- Parameters:
array- the array of values to join together, may be nullseparator- the separator character to use- Returns:
- the joined String,
nullif null array input - Since:
- 2.0
-
repeat
public static String repeat(String str, int repeat)
Repeat a Stringrepeattimes to form a new String.StringUtils.repeat(null, 2) = null StringUtils.repeat("", 0) = "" StringUtils.repeat("", 2) = "" StringUtils.repeat("a", 3) = "aaa" StringUtils.repeat("ab", 2) = "abab" StringUtils.repeat("a", -2) = ""- Parameters:
str- the String to repeat, may be nullrepeat- number of times to repeat str, negative treated as zero- Returns:
- a new String consisting of the original String repeated,
nullif null String input
-
length
public static int length(CharSequence cs)
Gets a CharSequence length or0if the CharSequence isnull.- Parameters:
cs- a CharSequence ornull- Returns:
- CharSequence length or
0if the CharSequence isnull. - Since:
- 2.4, 3.0 Changed signature from length(String) to length(CharSequence)
-
capitalize
public static String capitalize(String str)
Capitalizes a String changing the first character to title case as perCharacter.toTitleCase(int). No other characters are changed.A
nullinput String returnsnull.StringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt" StringUtils.capitalize("'cat'") = "'cat'"- Parameters:
str- the String to capitalize, may be null- Returns:
- the capitalized String,
nullif null String input - Since:
- 2.0
-
isAlpha
public static boolean isAlpha(String cs)
Checks if the CharSequence contains only Unicode letters.nullwill returnfalse. An empty CharSequence (length()=0) will returnfalse.StringUtils.isAlpha(null) = false StringUtils.isAlpha("") = false StringUtils.isAlpha(" ") = false StringUtils.isAlpha("abc") = true StringUtils.isAlpha("ab2c") = false StringUtils.isAlpha("ab-c") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif only contains letters, and is non-null- Since:
- 3.0 Changed signature from isAlpha(String) to isAlpha(CharSequence), 3.0 Changed "" to return false and not true
-
isAlphanumeric
public static boolean isAlphanumeric(String cs)
Checks if the CharSequence contains only Unicode letters or digits.nullwill returnfalse. An empty CharSequence (length()=0) will returnfalse.StringUtils.isAlphanumeric(null) = false StringUtils.isAlphanumeric("") = false StringUtils.isAlphanumeric(" ") = false StringUtils.isAlphanumeric("abc") = true StringUtils.isAlphanumeric("ab c") = false StringUtils.isAlphanumeric("ab2c") = true StringUtils.isAlphanumeric("ab-c") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif only contains letters or digits, and is non-null- Since:
- 3.0 Changed signature from isAlphanumeric(String) to isAlphanumeric(CharSequence), 3.0 Changed "" to return false and not true
-
isNumeric
public static boolean isNumeric(String cs)
Checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.nullwill returnfalse. An empty CharSequence (length()=0) will returnfalse.Note that the method does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range for int or long respectively.
StringUtils.isNumeric(null) = false StringUtils.isNumeric("") = false StringUtils.isNumeric(" ") = false StringUtils.isNumeric("123") = true StringUtils.isNumeric("१२३") = true StringUtils.isNumeric("12 3") = false StringUtils.isNumeric("ab2c") = false StringUtils.isNumeric("12-3") = false StringUtils.isNumeric("12.3") = false StringUtils.isNumeric("-123") = false StringUtils.isNumeric("+123") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif only contains digits, and is non-null- Since:
- 3.0 Changed signature from isNumeric(String) to isNumeric(CharSequence), 3.0 Changed "" to return false and not true
-
isAllLowerCase
public static boolean isAllLowerCase(String cs)
Checks if the CharSequence contains only lowercase characters.nullwill returnfalse. An empty CharSequence (length()=0) will returnfalse.StringUtils.isAllLowerCase(null) = false StringUtils.isAllLowerCase("") = false StringUtils.isAllLowerCase(" ") = false StringUtils.isAllLowerCase("abc") = true StringUtils.isAllLowerCase("abC") = false StringUtils.isAllLowerCase("ab c") = false StringUtils.isAllLowerCase("ab1c") = false StringUtils.isAllLowerCase("ab/c") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif only contains lowercase characters, and is non-null- Since:
- 2.5, 3.0 Changed signature from isAllLowerCase(String) to isAllLowerCase(CharSequence)
-
isAllUpperCase
public static boolean isAllUpperCase(String cs)
Checks if the CharSequence contains only uppercase characters.nullwill returnfalse. An empty String (length()=0) will returnfalse.StringUtils.isAllUpperCase(null) = false StringUtils.isAllUpperCase("") = false StringUtils.isAllUpperCase(" ") = false StringUtils.isAllUpperCase("ABC") = true StringUtils.isAllUpperCase("aBC") = false StringUtils.isAllUpperCase("A C") = false StringUtils.isAllUpperCase("A1C") = false StringUtils.isAllUpperCase("A/C") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif only contains uppercase characters, and is non-null- Since:
- 2.5, 3.0 Changed signature from isAllUpperCase(String) to isAllUpperCase(CharSequence)
-
removeZeroDecimal
public static String removeZeroDecimal(String str)
Removes all trailing zero decimals from the given String, assuming all decimals are zero and any zero decimals actually exist.A
nullinput String returnsnull.- Parameters:
str- the String to handle, may be null- Returns:
- string without trailing zero decimals
-
abbreviate
public static String abbreviate(String str, String abbrevMarker, int maxWidth)
Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "Now is the time for..."Specifically:
- If the number of characters in
stris less than or equal tomaxWidth, returnstr. - Else abbreviate it to
(substring(str, 0, max-3) + "..."). - If
maxWidthis less than4, throw anIllegalArgumentException. - In no case will it return a String of length greater than
maxWidth.
StringUtils.abbreviate(null, *) = null StringUtils.abbreviate("", 4) = "" StringUtils.abbreviate("abcdefg", 6) = "abc..." StringUtils.abbreviate("abcdefg", 7) = "abcdefg" StringUtils.abbreviate("abcdefg", 8) = "abcdefg" StringUtils.abbreviate("abcdefg", 4) = "a..." StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException- Parameters:
str- the String to check, may be nullabbrevMarker- the String indicate abbreviationmaxWidth- maximum length of result String, must be at least 4- Returns:
- abbreviated String,
nullif null String input - Throws:
IllegalArgumentException- if the width is too small- Since:
- 2.0
- If the number of characters in
-
-