StrSplitter

Origin

In Java’s String object, a split method is provided to split a string into an array using a certain string delimiter. However, sometimes we have different requirements for this operation that cannot be met by the default method, including:

  • Limit the number of divisions when splitting
  • Whether each string after splitting needs to remove the spaces at both ends
  • Whether to ignore blank segments
  • Split according to a fixed length
  • Split using regular expressions

Therefore, StrSplitter comes into being. StrSplitter consists of all static methods, which are convenient and fast to call.

Methods

Basic Methods

split: Split the string with numerous optional parameters, return the result as a List splitToArray: Split the string, return the result as an array splitByRegex: Split the string according to regular expressions splitByLength: Split the string according to a fixed length

Example:

String str1 = "a, ,efedsfs,   ddf";
// Arguments: string to be split, delimiter comma, 0 indicates no limit on the number of divisions, remove spaces at both ends, ignore blank items
List<String> split = StrSplitter.split(str1, ',', 0, true, true);

Special Methods

splitPath: Split the string using “/” as the delimiter splitPathToArray: Split the string using “/” as the delimiter, return the result as an array