Skip to main content
gamedev.net
🔒 Locked

[java] String help

Started by The C modest god Apr 8, 2006 at 6:32 AM 2 replies 850+ views
Original Post
The C modest god
The C modest god
I have a String with some lines and words in it. Lines are seprated by "\n" and words are seperated by one or more ' '. How do I read a line from a String? So I need a function the from string it will return me the first line, and maybe another function that will return me the String without the first line so I could read all the lines. From a line, how do I read a single word? and how do I read the line without the first word? I looked into StringBuffer and StringReader but didnt find anything that would help me with this problem. How can I implement this, have any idea? Thanks in advance.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
haegarr
haegarr
You could split the string into pieces like here
final String[] linesOfString = string.split("[\n]");for(int lineIndex=0; lineIndex<linesOfString.length; ++lineIndex) {   final String[] wordsOfLine = linesOfString[lineIndex].split("[ ]");   for(int wordIndex=0; wordIndex<wordsOfLine.length; ++wordIndex) {      // do something with wordsOfLine[wordIndex]   }}


You could furthur use the java.util.StringTokenizer class in a similar fashion as above.


Btw: java.lang.StringBuffer (and java.lang.StringBuilder, what is to be preferred in most cases since Java 1.5) are for making strings, not for parsing them.
The C modest god
The C modest god
Thanks.
I have another question, how do I create a popup window (not menu!).
Like when an error occurs in windows, you have a window you need to press send or close and you cant press the window that created the error window.
How do I do that in java?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.