Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Lourdes Edwards   on Sep 22 In Java Category.

  
Question Answered By: Marc Anderson   on Sep 22

If you just want to wrap your text without using StringTokenizer, I think
could give you a little help, here's a code  and comments :
// Retrieve your string
String newStringBig = textArea1.getText();
// Prepare buffer
StringBuffer buffer = new StringBuffer();
// Prepare the limits
int begin = 0;
int end = 0;
// Start the process until we got the end of the string
while (end != (newStringBig.length()))
{
// If the beginning of string  + maximum string (24) bigger than the
length of the string
if ((begin + 24) > (newStringBig.length() - 1))
{
// Okay, it's the end of string, use it as the end limit
end = newStringBig.length();
}
// If the rest of the string still bigger than 24 characters
else


// The end limit is the last occurrence of " " in our partial string
end = newStringBig.lastIndexOf(" ", (begin + 24));
}
// Append the partial string into buffer and add "+"
buffer.append(newStringBig.substring(begin, end) + "\n");
// Ok, we got the string...so just pass " " by adding one space
begin = end + 1;
}
// Show the result
System.out.println(buffer.toString());

Note :
If you have a word with the length  bigger than or equal 24, it's possible
the code would be broken.

Share: 

 

This Question has 3 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on How do i tokenize a string Or get search suggestion and latest updates.


Tagged: