Skip to content

Instantly share code, notes, and snippets.

@bassemZohdy
Created November 30, 2015 10:16
Show Gist options
  • Save bassemZohdy/c0feb9e36010f62e237c to your computer and use it in GitHub Desktop.
Save bassemZohdy/c0feb9e36010f62e237c to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
public class StringToCharSequences implements Function<String, CharSequence[]> {
final private char delimiter;
public StringToCharSequences(char delimiter) {
this.delimiter = delimiter;
}
@Override
public CharSequence[] apply(String s) {
AtomicInteger i = new AtomicInteger(0);
List<StringBuilder> list = new ArrayList<>();
list.add(new StringBuilder());
s.chars().forEachOrdered(c -> {
if (c == delimiter) {
i.incrementAndGet();
list.add(new StringBuilder());
} else {
list.get(i.get()).append((char) c);
}
});
return list.toArray(new CharSequence[list.size()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment