Package com.tccc.kos.commons.util.misc
Class StringUtil
java.lang.Object
com.tccc.kos.commons.util.misc.StringUtil
Utility class that handles various string-based operations.
Many are similar to Apache commons-lang3 StringUtils methods,
however we don't want to include that large library for just a few methods.
- Since:
- 1.0
- Version:
- 2023-08-25
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringappendIfMissing(String str, String suffix) Appends the suffix to the end of the string if the string does not already end with it.static StringcalcParentDir(String fileOrDirPath) Given a file or directory path, return its parent directory.static booleanReturn true if the specified strings are equal.static StringfixFileSeparators(String inputPath) Replaces all "\" characters with "/" in the given string.static StringgetClassName(Class<?> clazz) Returns the simple class name, but includes enclosing class if nested.static StringgetClassName(String fullClassName) Same as above, except the class name is passed in.static StringgetNotNull(String str1, String str2) Given two strings, return the first if not null, otherwise return the second.static booleanhasValue(CharSequence charSequence) Determines if the given CharSequence has a value or not.static booleanisBlank(CharSequence charSequence) Checks if a CharSequence is empty (""), null or whitespace only.static booleanisEmpty(CharSequence charSequence) Checks if a CharSequence is empty ("") or null.static booleanReturn true if the string starts with { or [ which indicates that the string is a json payload.static intlength(CharSequence charSequence) Gets a CharSequence length or0if the CharSequence isnull.static StringEnsures that the given string is not blank.static StringEnsures that the given string is not null.static String[]Null-safe way to split a string into its individual components.static StringTrims the given string, or returns null if input is null.
-
Constructor Details
-
StringUtil
public StringUtil()
-
-
Method Details
-
length
Gets a CharSequence length or0if the CharSequence isnull.- Parameters:
charSequence- a CharSequence ornull- Returns:
- CharSequence length or
0if the CharSequence isnull.
-
hasValue
Determines if the given CharSequence has a value or not.- Parameters:
charSequence- the CharSequence to check, may be null- Returns:
- true if the charSequence has any non-whitespace characters
-
isEmpty
Checks if a CharSequence is empty ("") or null. Taken from Apache commons-lang3 StringUtils method of the same name.Str.isEmpty(null) = true Str.isEmpty("") = true Str.isEmpty(" ") = false Str.isEmpty("bob") = false Str.isEmpty(" bob ") = false- Parameters:
charSequence- the CharSequence to check, may be null- Returns:
trueif the CharSequence is empty or null
-
isBlank
Checks if a CharSequence is empty (""), null or whitespace only. Whitespace is defined byCharacter.isWhitespace(char).Str.isBlank(null) = true Str.isBlank("") = true Str.isBlank(" ") = true Str.isBlank(" \t\r\n") = true Str.isBlank("bob") = false Str.isBlank(" bob ") = false- Parameters:
charSequence- the CharSequence to check, may be null- Returns:
trueif the CharSequence is null, empty or whitespace only
-
notNull
Ensures that the given string is not null.- Parameters:
str- the input string- Returns:
- the input string if it's not null, otherwise the empty string
-
notBlank
Ensures that the given string is not blank.- Parameters:
str- the input string- Returns:
- the input string if it's not blank, otherwise null
-
trim
Trims the given string, or returns null if input is null.- Parameters:
str- the input string- Returns:
- the trimmed string
-
appendIfMissing
Appends the suffix to the end of the string if the string does not already end with it. Based on the Apache commons-lang3 StringUtils method of the same name.Str.appendIfMissing(null, null) = null Str.appendIfMissing("abc", null) = "abc" Str.appendIfMissing("", "xyz") = "xyz" Str.appendIfMissing("abc", "xyz") = "abcxyz" Str.appendIfMissing("abcxyz", "xyz") = "abcxyz" Str.appendIfMissing("abcXYZ", "xyz") = "abcXYZxyz"- Parameters:
str- the input stringsuffix- the suffix to check for and append to the end of the string- Returns:
- a new String if suffix was appended, the same string otherwise.
-
calcParentDir
Given a file or directory path, return its parent directory.Str.calcParentDir(null) = null Str.calcParentDir("/") = null Str.calcParentDir("/index.html") = "/" Str.calcParentDir("/foo/") = "/" Str.calcParentDir("/foo/bar/") = "/foo/" Str.calcParentDir("/foo/bar/image.jpg") = "/foo/bar/"- Parameters:
fileOrDirPath- complete file or directory name- Returns:
- parent directory, or null if top-level passed in
-
fixFileSeparators
Replaces all "\" characters with "/" in the given string. Related to the operating system's "file separator", which is retrieved byFileSystems.getDefault().getSeparator().- Parameters:
inputPath- input file or directory path- Returns:
- converted string
-
split
Null-safe way to split a string into its individual components. Unlike the default String.split() method, trailing empty strings ARE included in the resulting array.Str.split(null, ",") = [] Str.split("1,2,3", ",") = ["1", "2", "3"] Str.split("1:2:3", ":") = ["1", "2", "3"] Str.split(",2,3", ",") = ["", "2", "3"] : missing first string Str.split("1,,3", ",") = ["1", "", "3"] : missing middle string Str.split("1,2,", ",") = ["1", "2", "" ] : missing trailing string- Parameters:
input- input textseparator- separator string (not a regular expression!)
-
isJson
Return true if the string starts with { or [ which indicates that the string is a json payload.- Parameters:
input- the string to check
-
equals
Return true if the specified strings are equal. Handles comparing nulls.- Parameters:
str1- input stringstr2- input string
-
getClassName
Returns the simple class name, but includes enclosing class if nested. For example, "com.tccc.kos...Trouble$View" returns "Trouble$View". This is different from clazz.getSimpleName(), which simply returns "View". -
getClassName
Same as above, except the class name is passed in. -
getNotNull
Given two strings, return the first if not null, otherwise return the second.
-