How do you split a string?

How could I split a string into a List 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 :frowning:


    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.

you could roll your own, or use something from java.text: http://java.sun.com/javase/6/docs/api/java/text/package-use.html - BreakIterator perhaps - not sure. Never used it …

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 .

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.

I’d recommend using a StringTokenizer. Then you just do something like adding each token to a temporary string until that string is above a certain length, then you print it.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html

I don’t have any recommendations so the following might not be fair :stuck_out_tongue:

[quote]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.
[/quote]

;D
Fair enough. Probably this happened because I haven’t used StringTokenizer for… 5 years. :o

I had to spend a bit of time remembering what it was called. :stuck_out_tongue:

So try this instead:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/package-summary.html

[quote]How do you split a string?
[/quote]
you already asked your girlfriend? ;D

if not:
the java6 answer to StringTokenizer is Scanner (http://java.sun.com/javase/6/docs/api/java/util/Scanner.html)

I have already got some code that works, but it splits words too.

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.

I get where your coming from.

Apprantly Abuse has the answer

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.

That very useful to know. I’ve definitely spent a lot of time rolling my own on many occasions.

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 ::slight_smile:

o.O?

Hell yeah, it’s like cheap hydraulics. ;D

NIH.

Homework!?

No, its for a chat bubble, lol

:slight_smile: I just guessed, because you didn’t want to use java.awt.font…