title: Extra word appended to smart substring algorithm
body: <p>I am working on a CareerCup question:</p>
  
  <blockquote>
    <p>"Smart substring"  Write a function that takes maximum 30 characters
    from a string but without cutting the words. </p>
    
    <p>Full description:  "Featuring stylish rooms and moorings for
    recreation boats, Room Mate Aitana is a designer hotel built in 2013
    on an island in the IJ River in Amsterdam." </p>
    
    <p>First 30 characters:  "Featuring stylish rooms and mo" </p>
    
    <p>Smarter approach (max 30 characters, no words are broken):  "Featuring
    stylish rooms and"</p>
  </blockquote>
  
  <p>Here is my code. My logic is, I only append the string if after the appending, it is less than 30 characters.</p>
  
  <pre><code>public static String substring(String sentence) {
      StringBuffer subString = new StringBuffer();
      String[] splitStr = sentence.split(" ");
      for(String s : splitStr){
          if(subString.append(" " + s).length() &lt; 30) {
              continue;
          } else {
              return subString.toString();
          }
      }
      return subString.toString();
  }
  </code></pre>
  
  <p>This is my input:</p>
  
  <pre><code>    String s = "Featuring a stylish room thatsssss is almost";
      System.out.println("Count: " + substring(s).length());
      System.out.println("Result: " + substring(s));
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Count: 35
  Result:  Featuring a stylish room thatsssss
  </code></pre>
  
  <p>The fifth word <code>thatsssss</code> is tripping my algorithm up, but I guess that makes sense because in the <code>else-statement</code>, the word is already appended in the conditional check. </p>
  
  <p>My question is - how do I both check for the conditional of <em>after the appending</em> and have it not append in the return?</p>