Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How do you split a string?  (Read 3761 times)
0 Members and 1 Guest are viewing this topic.
Offline Renoria

Junior Member




...


« 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 Sad
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.
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #1 - Posted 2008-12-12 10:53:56 »

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 ...

Offline zammbi

JGO Coder


Medals: 4



« 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 .

Current project - Rename and Sort
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Member


Medals: 1


shiny.


« 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.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2008-12-12 18:40:04 »

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

See my work:
OTC Software
Offline Mr_Light

Senior Member


Medals: 1


shiny.


« Reply #5 - Posted 2008-12-12 19:50:36 »


I don't have any recommendations  so the following might not be fair 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.

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.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #6 - Posted 2008-12-12 20:26:55 »

I don't have any recommendations  so the following might not be fair Tongue

Grin
Fair enough. Probably this happened because I haven't used StringTokenizer for... 5 years.  Shocked

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

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

See my work:
OTC Software
Offline bienator

Senior Member




OutOfCoffeeException


« Reply #7 - Posted 2008-12-12 20:32:20 »

Quote
How do you split a string?
you already asked your girlfriend?  Grin

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

Offline Renoria

Junior Member




...


« Reply #8 - Posted 2008-12-13 00:00:38 »

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

JGO Knight


Medals: 12


falling into the abyss of reality


« 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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Member


Medals: 1


shiny.


« Reply #10 - Posted 2008-12-13 04:50:06 »

Grin
Fair enough. Probably this happened because I haven't used StringTokenizer for... 5 years.  Shocked
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.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« 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.

See my work:
OTC Software
Offline Renoria

Junior Member




...


« 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...
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« 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  Roll Eyes

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Renoria

Junior Member




...


« Reply #14 - Posted 2008-12-14 03:30:46 »

yep, I too opt for square wheels every time I buy a new car  Roll Eyes

o.O?
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #15 - Posted 2008-12-14 04:02:58 »

yep, I too opt for square wheels every time I buy a new car  Roll Eyes
Hell yeah, it's like cheap hydraulics.  Grin

See my work:
OTC Software
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #16 - Posted 2008-12-14 10:56:14 »

o.O?
<a href="http://en.wikipedia.org/wiki/Not_Invented_Here">NIH</a>.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline cylab

JGO Ninja


Medals: 50



« Reply #17 - Posted 2008-12-15 10:11:56 »

Why would you need this (...)

Homework!?

Mathias - I Know What [you] Did Last Summer!
Offline Renoria

Junior Member




...


« Reply #18 - Posted 2008-12-15 12:39:55 »

Homework!?

No, its for a chat bubble, lol
Offline cylab

JGO Ninja


Medals: 50



« Reply #19 - Posted 2008-12-15 15:49:56 »

Smiley I just guessed, because you didn't want to use java.awt.font...

Mathias - I Know What [you] Did Last Summer!
Offline Renoria

Junior Member




...


« Reply #20 - Posted 2008-12-15 22:36:16 »

Smiley 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...
Offline Wildern

Junior Member





« Reply #21 - Posted 2008-12-16 00:41:54 »

From the docs located here
Quote
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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!