Guest User

Untitled

a guest
Oct 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. public List<String> hirschisWordWrapper(String text) {
  2. text = text.trim();
  3. List<String> words = new ArrayList<>();
  4. for (String word : Arrays.asList(text.split(" "))) {
  5. words.addAll(splitLongWordIntoSubStrings(word, Constants.MAX_STRING_LENGTH_BUBBLES));
  6. }
  7. return concatWordsIntoLines(words, Constants.MAX_STRING_LENGTH_BUBBLES);
  8. }
  9.  
  10. private List<String> splitLongWordIntoSubStrings(String word, int maxLength) {
  11. List<String> intermediateList = new ArrayList<>();
  12. while (word.length() > maxLength) {
  13. intermediateList.add(word.substring(0, maxLength));
  14. word = word.substring(maxLength);
  15. }
  16. intermediateList.add(word);
  17. return intermediateList;
  18. }
  19.  
  20. private List<String> concatWordsIntoLines(List<String> words, int maxLineLength) {
  21. List<String> lines = new ArrayList<>();
  22. String line = words.get(0);
  23. for (String word : words.subList(1, words.size())) {
  24. if (line.length() + word.length() < maxLineLength) {
  25. line = line + " " + word;
  26. } else {
  27. lines.add(line);
  28. line = word;
  29. }
  30. }
  31. lines.add(line);
  32. return lines;
  33. }
Add Comment
Please, Sign In to add comment