StrUtil
Introduction
This tool is similar to StringUtil
in Apache Commons Lang, the difference being that StrUtil
is shorter and more通俗易懂. The commonly used methods such as isBlank
, isNotBlank
, isEmpty
, isNotEmpty
are not explained here, as they are straightforward for checking whether a string is empty or not. Instead, I will highlight some unique features below.
Methods
1. hasBlank
and hasEmpty
methods
Given a list of strings, these methods return true if any of the strings are empty or blank (e.g., for checking whether any fields in a web form are empty). The difference between the two methods is that hasEmpty
only checks for null or an empty string ("") while hasBlank
also includes invisible characters as empty.
2. removePrefix
and removeSuffix
methods
These methods are used to remove the prefix or suffix of a string, making it easier to handle file names, for example.
String fileName = StrUtil.removeSuffix("pretty_girl.jpg", ".jpg") //fileName -> pretty_girl
There are also versions that ignore case, removePrefixIgnoreCase
and removeSuffixIgnoreCase
, which are useful when dealing with strings.
3. sub
method
This method is noteworthy as it provides additional functionality compared to the built-in substring
method in Java. It allows you to specify negative indices, which means it can start from the end of the string. This functionality is borrowed from Python, making it convenient to handle substring operations. It also automatically corrects any confusion in the start and end indices to avoid exceptions.
For example:
String str = "abcdefgh";
String strSub1 = StrUtil.sub(str, 2, 3); //strSub1 -> c
String strSub2 = StrUtil.sub(str, 2, -3); //strSub2 -> cde
String strSub3 = StrUtil.sub(str, 3, 2); //strSub2 -> c
Please note that index -1 does not include the last character when using the sub
method, so be careful when using it. If you want to obtain the substring starting from the last character to a certain position, it is recommended to use the subSuf
method.
4. str
and bytes
methods
These methods wrap the Java standard library method String.getBytes(String charsetName)
to provide additional functionality and error handling, as this method may throw an UnsupportedEncodingException
. These methods allow you to specify the character set when converting a string to bytes or vice versa.
5. format
method
This is my favorite method, as it provides a convenient string templating feature similar to that found in slf4j. It uses placeholders (e.g., {}) for variables in the template string and replaces them with the corresponding values passed as arguments when formatting the string. This allows you to easily create readable and maintainable log messages without worrying about string concatenation or handling exceptions related to unsupported encodings. It can handle multiple arguments and even objects that have their own toString() method.
6. Constants defined
For convenience, some common constants related to strings and HTML have been defined within this tool class, including dot, empty string, newline characters, and various HTML escape characters that may be commonly used in web development or data processing tasks.