I
agree that you will want to break the lines at whitespace (or use some
hyphenation rules), rather than cut mid-word.
You
may not be using the FontMetrics that java.swing.text.Utilities (in 1.4 swing
seems to be under javax) requires,
if it
is a simple Dialog box (then again....).
A look
at the source code for getBreakLocation() gives you the general idea - iterate
through the string/charArray for
the
required line length - then go back to the last whitespace (copy as you go -
keep a pointer to the last whitespace etc).
db
____________________________________________________-----Original Message-----
From: Roger Glover [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, January 21, 2003 11:27 AM
To: JDJList
Subject: [jdjlist] RE: String segmentation??? (was: RE: Slow: string += s tring)I may be missing something here, but what the hey...Wouldn't it be easier (and likely very efficient, if not optimal) to use java.swing.text.Segment and java.swing.text.Utilities.getBreakLocation() ?-- Roger Gloverglover_roger____________________________________________________-----Original Message-----
From: Greg Nudelman [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 20, 2003 4:05 PM
To: JDJList
Subject: [jdjlist] RE: String segmentation??? (was: RE: Slow: string += s tring)I have to test this, but I think substring is probably adequate for most purposes. The other way to go would be to is to use a char array, as below. I don't know which one is faster -- my hunch is it depends on the size and number of String objects created.char[] c = myString.toCharArray();int paragraphLength = 35;int fullArrayLength = c.length;Vector splitStrings = new Vector(fullArrayLength/paragraphLength + 1);StringBuffer splitString = null;for(int j = 0; j < fullArrayLength/paragraphLength; j++) {splitString = new StringBuffer();for(int i = 0; i < paragraphLength; i++) {splitString.append(c[j*paragraphLength + i]);}splitStrings.addElement(splitString.toString());}You guys might want to test this one if you want to use it -- I may have some off by 1 errors or something...Greg____________________________________________________-----Original Message-----
From: Patrick Li [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 20, 2003 1:21 PM
To: JDJList
Subject: [jdjlist] String segmentation??? (was: RE: Slow: string += string)While we are talking about string concatenation, what about truncation? Is there a more efficient way to break a long string into segments than using substring()? For example, if I read a paragraph into a string and then wants to break it into line depending on the size of dialog box that will display the paragraph. What is the most efficient way of doing that?Thanks in advancePatrick Li____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
Be respectful! Clean up your posts before replying
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
Be respectful! Clean up your posts before replying
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
Be respectful! Clean up your posts before replying
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
Be respectful! Clean up your posts before replying
____________________________________________________
