Renoria
|
 |
«
Posted
2008-12-12 10:44:19 » |
|
How could I split a string into a List<String> not breaking words, but breaking apart really long words? Example: "This is a very long text" Becomes "This is a" "very long" "text" But a string like "asdfawerawefefrafewa" Becomes "asdfaw" "erawefe" "frafewa" even though its got no spaces. I've already got this code but it splits words too  1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public ArrayList<String> splitLines(String s) { ArrayList<String> ret = new ArrayList<String>(); char c; String ln = ""; FontMetrics fm = Console.getWindow().getGraphics().getFontMetrics(); for (int i = 0; i < s.length(); i++) { c = s.charAt(i); if (fm.stringWidth(ln) >= maxWidth) { ret.add(new String(ln)); ln = ""; } ln += c; } return ret; } |
Thanks.
|
|
|
|
|
zammbi
|
 |
«
Reply #2 - Posted
2008-12-12 11:11:58 » |
|
I believe I have made such a thing ages back.
How I would do is look at the X character (where you want to break), and look if its a space otherwise keep looking at the previous space. If its a space then break it up. If theres no spaces, then go back to the x position and look for the next space and break it up there.
Hope that helps.
Edit: Woops I read wrong what you wanted, adjusted what I wrote .
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Mr_Light
|
 |
«
Reply #3 - Posted
2008-12-12 16:26:14 » |
|
Why would you need this, unless your using Monospaced font. Else your going to have to go through the letter widths and split based on that. Or split on spaces and take word wides or do somethign smart with spaces and used a justified layout.
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
|
Mr_Light
|
 |
«
Reply #5 - Posted
2008-12-12 19:50:36 » |
|
I don't have any recommendations so the following might not be fair  StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
|
|
Renoria
|
 |
«
Reply #8 - Posted
2008-12-13 00:00:38 » |
|
I have already got some code that works, but it splits words too.
|
|
|
|
Abuse
|
 |
«
Reply #9 - Posted
2008-12-13 01:07:40 » |
|
Text wrapping is a complex problem that occurs in almost all applications. Ergo, the JDK contains a package for dealing with it.
Have a look in the java.awt.font package, in particular the LineBreakMeasurer class.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Mr_Light
|
 |
«
Reply #10 - Posted
2008-12-13 04:50:06 » |
|
 Fair enough. Probably this happened because I haven't used StringTokenizer for... 5 years.  I get where your coming from. Apprantly Abuse has the answer Have a look in the java.awt.font package, in particular the LineBreakMeasurer class.
Seem to be there since adleast 1.4.2 considering the javadoc. I certainly must have missed it back then. Makes sense I suppose, though LineBreakMeasurer isn't the first name that pops up in my head.
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
Eli Delventhal
|
 |
«
Reply #11 - Posted
2008-12-13 05:51:01 » |
|
That very useful to know. I've definitely spent a lot of time rolling my own on many occasions.
|
|
|
|
Renoria
|
 |
«
Reply #12 - Posted
2008-12-14 00:40:26 » |
|
Text wrapping is a complex problem that occurs in almost all applications. Ergo, the JDK contains a package for dealing with it.
Have a look in the java.awt.font package, in particular the LineBreakMeasurer class.
Thanks for this, but I'd rather use my own code...
|
|
|
|
Abuse
|
 |
«
Reply #13 - Posted
2008-12-14 01:56:49 » |
|
Thanks for this, but I'd rather use my own code...
yep, I too opt for square wheels every time I buy a new car 
|
|
|
|
Renoria
|
 |
«
Reply #14 - Posted
2008-12-14 03:30:46 » |
|
yep, I too opt for square wheels every time I buy a new car  o.O?
|
|
|
|
Eli Delventhal
|
 |
«
Reply #15 - Posted
2008-12-14 04:02:58 » |
|
yep, I too opt for square wheels every time I buy a new car  Hell yeah, it's like cheap hydraulics. 
|
|
|
|
|
cylab
|
 |
«
Reply #17 - Posted
2008-12-15 10:11:56 » |
|
Why would you need this (...)
Homework!?
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
Renoria
|
 |
«
Reply #18 - Posted
2008-12-15 12:39:55 » |
|
Homework!?
No, its for a chat bubble, lol
|
|
|
|
cylab
|
 |
«
Reply #19 - Posted
2008-12-15 15:49:56 » |
|
 I just guessed, because you didn't want to use java.awt.font...
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
Renoria
|
 |
«
Reply #20 - Posted
2008-12-15 22:36:16 » |
|
 I just guessed, because you didn't want to use java.awt.font... I would use it if I knew how... The JavaDocs don't show enough information...
|
|
|
|
Wildern
|
 |
«
Reply #21 - Posted
2008-12-16 00:41:54 » |
|
From the docs located here Segments of text are obtained by calling the method nextLayout, which returns a TextLayout representing the text that fits within the wrapping width. The nextLayout method moves the current position to the end of the layout returned from nextLayout.
LineBreakMeasurer implements the most commonly used line-breaking policy: Every word that fits within the wrapping width is placed on the line. If the first word does not fit, then all of the characters that fit within the wrapping width are placed on the line. At least one character is placed on each line. Which is followed shortly by two examples of how to use it. The TextLayout object contains the text that will fit on one line, and it knows how to draw itself, but you can call toString() on it to find out the actual text.
|
|
|
|
|