• Post Reply Bookmark Topic Watch Topic
  • New Topic

split a sentence to a an arraylist of smaller sentence-part, with the max length of each "part"?  RSS feed

 
Niklas Karlsson
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
is there a "best way" to do the following? :

I receive a long sentence and want to display on a fixed view of 20 char.
and i can hava unlimited rows.

how do i fix this "best"?
"
have a:
String longSentence = "This is a long very long text that is more than twenty chars";
and want a ArrayList-result, with:
[0] : "This is a long very"
[1] : "long text that is"
[2] : "more than twenty"
[3] : "chars"
"


Im stuck in a "over do it" scenario, so i cant get i... :-(

any ideas?

Ragards
Niklas
 
Niklas Karlsson
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh google is you friend if you find the right keywords....

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks good!  Thanks for posting your solution.

I notice that your code will put an extra space at the end of the text in each element of the array.  Is that what you intended?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am rather less impressed by that. It uses legacy code (String Tokenizer) and it uses the wrong line end; \n is incorrect for Windows® users. Please show us where it came from.
Please avoid coloured text, which many people find difficult to read. Orange is particularly difficult against the buff background of this website.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!