Computing.Net > Forums > Programming > Java string question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java string question

Reply to Message Icon

Name: rjackson2090
Date: February 24, 2004 at 19:06:36 Pacific
OS: Windows XP Professional
CPU/Ram: Amd Duron 1.3 at ~1.4 ,5
Comment:

hey, i have a quick question about strings

im using the stringbuffer class to store letters in a string, i was wondering if there was an easy way to distinguish parts of the string say if i have

StringBuffer a
and i add the words "one" then "two"
a would contain "onetwo" my question is if i wanted to extract that one two, how could i extract them without doing it character by character(because i wouldnt know where the end of the word was)

My solution was to add a ',' between each one i putting in i.e. "one,two" but i dont know if this is the most efficent way, thanks for your help in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: wharfie
Date: March 1, 2004 at 14:46:48 Pacific
Reply:

I think your on the right track with using the delimiter. You can then use the StingTokenizer class if your using Java prior to 1.4 or String.split() if your using Java 1.4.x.

See API documentation;

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

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

For example;

StringBuffer mySB = new StringBuffer("one,two,three");
String myStr = mSB.toSting();
String[] words = myStr.split(",");
for (int i=0; i < words.length; i++){
System.out.println(words[i]);
}


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java string question

Java strings www.computing.net/answers/programming/java-strings/9620.html

Java String Problem www.computing.net/answers/programming/java-string-problem/9097.html

Java Method Question www.computing.net/answers/programming/java-method-question/14684.html