code: protected String splitNear (String text, int pos)
      {
          if (pos >= text.length()) {
              return text;
          }
  
          int forward = text.indexOf(' ', pos);
          int backward = text.lastIndexOf(' ', pos);
  
          int newpos = (Math.abs(pos - forward) < Math.abs(pos - backward)) ? forward : backward;
  
          // if we couldn't find a decent place to split, just do it wherever
          if (newpos == -1) {
              newpos = pos;
  
          } else {
              // actually split the space onto the first part
              newpos++;
          }
          return text.substring(0, newpos);
      }
docs: Split the text at the space nearest the specified location.