title: Breaking string into multiple lines in Java
body: <p>I have a single line string of length n, which I want to split into maximum of 3 lines. Each line can have a maximum of 45 chars, after which I want to add a new-line char ("\n"). The 3rd line can have a maximum of 42 chars after which I need to include 3 dots (...) if the string goes beyond that, thus making the total characters in the 3rd line 45 as well.</p>
  
  <p>The condition is that the new line character should not be added in the middle of a word. How do I do this efficiently? This operation is just a small part of the entire program, but will be called repeatedly. So I'm not sure if I should actually bother about the efficiency.</p>
  
  <p>What I'm doing right now is that I first figure out where the spaces between words are and then add it to a List. I then iterate through the list and find 3 indices each representing the end word of each line. So the first index will be the space closest to 45, the next closest to 90, and the third closest to 135. I then use these indices to split the actual string, and add "\n" and "..." respectively. This is my code:</p>
  
  <pre><code>//maxCharsPerLine will be 45
  public String splitString(String input, int maxCharsPerLine){    
          String output = "";
          ArrayList&lt;Integer&gt; spaces = new ArrayList&lt;Integer&gt;();
  
          // Logic to figure out after which word the sentence should be split so that we don't split in middle of a word
          for(int index = 0; index &lt; input.length(); index++){
              if(input.charAt(index)==' '){
                  spaces.add(index);
              }
          }
          //add index of last word of string
          spaces.add(input.length());
  
          int index1 = 0; int index2 = 0; int index3 = 0;
          for(Integer index : spaces){
              // find word closest to and less than maxCharsPerLine. This index will be used to find the last word in line1
              if(index&lt;=maxCharsPerLine)
                  index1 = index;
              // find word closest to and less than 2*maxCharsPerLine. This index will be used to find the last word in line2
              else if(index&lt;=2*maxCharsPerLine)
                  index2 = index;
              // find word closest to and less than 3*maxCharsPerLine, but exclude 3 chars for adding the dots (...). This index will be used to find the last word in line3
              else if(index&lt;=(3*maxCharsPerLine)-3)
                  index3 = index;
          }
  
          if(input.length()&gt;maxCharsPerLine){
              if(index1 &gt; 0)
                  output = input.substring(0, index1);
              if(index2 &gt; 0)
                  output += "\n"+input.substring(index1+1, index2);
              if(index3 &gt; 0){
                  output += "\n"+input.substring(index2+1, index3);
                  if(input.length()&gt;3*maxCharsPerLine)
                      output += "...";    
              }
          }
          //if length of input is &lt; 45, just return the input
          else
              output = input;
  
          return output;
      }
  </code></pre>
  
  <p>Not sure in which scenarios this will fail. Is there a better way to do this?</p>
  
  <p>Thanks.</p>