Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<String> hirschisWordWrapper(String text) {
- text = text.trim();
- List<String> words = new ArrayList<>();
- for (String word : Arrays.asList(text.split(" "))) {
- words.addAll(splitLongWordIntoSubStrings(word, Constants.MAX_STRING_LENGTH_BUBBLES));
- }
- return concatWordsIntoLines(words, Constants.MAX_STRING_LENGTH_BUBBLES);
- }
- private List<String> splitLongWordIntoSubStrings(String word, int maxLength) {
- List<String> intermediateList = new ArrayList<>();
- while (word.length() > maxLength) {
- intermediateList.add(word.substring(0, maxLength));
- word = word.substring(maxLength);
- }
- intermediateList.add(word);
- return intermediateList;
- }
- private List<String> concatWordsIntoLines(List<String> words, int maxLineLength) {
- List<String> lines = new ArrayList<>();
- String line = words.get(0);
- for (String word : words.subList(1, words.size())) {
- if (line.length() + word.length() < maxLineLength) {
- line = line + " " + word;
- } else {
- lines.add(line);
- line = word;
- }
- }
- lines.add(line);
- return lines;
- }
Add Comment
Please, Sign In to add comment