Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Splits the given string by the given splitter string, keeping empty strings
- * if there are zero characters between splits
- * @param string The input string to split
- * @param split The splitter value
- * @return A list of split elements
- * @author REghZy
- */
- @Nonnull
- public static ArrayList<String> splitAsList(@Nullable String string, @Nullable String split) {
- int srcLen, tarLen;
- if (string == null || (srcLen = string.length()) == 0)
- return new ArrayList<String>();
- if (split == null || (tarLen = split.length()) == 0)
- return Linq.of(charToStringArray(string.toCharArray())).toList();
- ArrayList<String> list = new ArrayList<String>();
- int i = -tarLen, last = 0;
- while ((i = string.indexOf(split, i + tarLen)) != -1) {
- list.add(string.substring(last, i));
- last = i + tarLen;
- }
- // using '<=' instead of '<' to keep the empty string element if the split is
- // at the very end of the string (because 'last' will be equal to 'srcLen' in this case)
- // e.g. splitAsList("hi__lol__") with '<=' gives ["hi", "lol", ""], whereas '<' gives ["hi", "lol"]
- // Could add a "keepTrailingSplits" parameter, and replace below with (keepTrailingSplits ? (last <= srcLen) : (last < srcLen))
- if (last <= srcLen) {
- list.add(string.substring(last));
- }
- return list;
- }
Advertisement
Add Comment
Please, Sign In to add comment