text-area-text-to-be-split-with-conditions repeated

Posted by desmiserables on Stack Overflow See other posts from Stack Overflow or by desmiserables
Published on 2009-06-21T07:13:07Z Indexed on 2010/03/13 1:07 UTC
Read the original article Hit count: 397

I have a text area wherein i have limited the user from entering more that 15 characters in one line as I want to get the free flow text separated into substrings of max limit 15 characters and assign each line an order number. This is what I was doing in my java class:

int interval = 15;                
items = new ArrayList();
 TextItem item = null;
 for (int i = 0; i < text.length(); i = i + interval) {
  item = new TextItem ();
  item.setOrder(i);

  if (i + interval < text.length()) {
    item.setSubText(text.substring(i, i + interval));
    items.add(item);
  } 
  else {
    item.setSubText(text.substring(i));
    items.add(item);
  }
}

Now it works properly unless the user presses the enter key. Whenever the user presses the enter key I want to make that line as a new item having only that part as the subText.

I can check whether my text.substring(i, i + interval) contains any "\n" and split till there but the problem is to get the remaining characters after "\n" till next 15 or till next "\n" and set proper order and subText.

© Stack Overflow or respective owner

Related posts about java

Related posts about string-manipulation