import java.util.ArrayList;
import java.util.List;
public class SplitUsingAnotherMethodBecauseBossLikesWastingEveryonesTime {
public static void main(String[] args) {
System.out.println(split("Why would anyone want to write their own String split function in Java?", ' '));
System.out.println(split("The|Split|Method|Is|Way|More|Flexible||", '|'));
}
private static List<String> split(String input, char delimiter) {
List<String> result = new ArrayList<>();
int idx = 0;
int next;
do {
next = input.indexOf(delimiter, idx);
if (next > -1) {
result.add(input.substring(idx, next));
idx = next + 1;
}
} while(next > -1);
result.add(input.substring(idx));
return result;
}
}
Outputs...
[Why, would, anyone, want, to, write, their, own, String, split, function, in, Java?]
[The, Split, Method, Is, Way, More, Flexible, , ]