This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes splitting a StringBuffer into tokens? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "splitting a StringBuffer into tokens?" Watch "splitting a StringBuffer into tokens?" New topic
Author

splitting a StringBuffer into tokens?

Jack Lord
Greenhorn

Joined: Jun 04, 2004
Posts: 11
Hello.

I'm just getting started with Java and having a bit of difficulty using Strings and StringBuffers. I decided to make a class that would take a string (this was before I knew there two types in Java!) representing a name, split it into its constituent parts, capitalise the first letter of each of these and put them into an array. So, for example, the string "jack fraser lord" would be used to create an array ["Jack", "Fraser", "Lord"]

The code I came up with was:





This seems quite long winded to me but as far as I can tell I need to operate on a String type to split my string into tokens, and then operate on a StringBuffer to capitalise the first letter of each token. Is that right? I wondered if there was an easy way to split a StringBuffer into an array of StringBuffers that I could directly operate on - because at the moment I seem to be using Strings just so that I can get the tokens.

Sorry, that was a bit longer than I expected!

--Split the long comment to limit screen width - Carl
[ June 08, 2004: Message edited by: Carl Trusiak ]
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
Fortunately, there is a StringTokenizer class which will fulfil your needs very easily.

Ken
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
I don't see any "split" method in the String class - where did that come from?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[JL]: This seems quite long winded to me but as far as I can tell I need to operate on a String type to split my string into tokens, and then operate on a StringBuffer to capitalise the first letter of each token. Is that right?

Yes. There are other ways to write it I suppose - e.g.

or

That will look a little shorter. But behind the scenes it's really doing the same thing: the + operator creates a StringBuffer, appends the two strings, and returns a single joined String.

I wondered if there was an easy way to split a StringBuffer into an array of StringBuffers that I could directly operate on - because at the moment I seem to be using Strings just so that I can get the tokens.

No. You could conceivably convert the original ong String into a char[] array, then loop through the array and every time you find a space, convert the next non-space char you find into uppercase. Then convert the whole char[] array into a new String.

[KR]: Fortunately, there is a StringTokenizer class which will fulfil your needs very easily.

I don't see how this offers any improvements over the code we can get with split() here.

[DW]: I don't see any "split" method in the String class - where did that come from?

Ummm... Sun? It's been in the String class since JDK 1.4, which came out two years ago. Sun's main documentation page is here. You can find info from different versions on this page. Note 1.5 is now out in beta.
[ June 06, 2004: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
Thanks Jim - I've been using SE ver. 1.3.1 documentation (but have 1.4 installed). I need to make sure I use the right documentation.
Jack Lord
Greenhorn

Joined: Jun 04, 2004
Posts: 11
OK, thanks, that's cleared it up for me I think. I was just having a bit of difficulty getting my head around two different string classes but I'm becoming used to it now - I do like knowing what goes on beneath the surface so these things tend to bug me for ages. Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: splitting a StringBuffer into tokens?
 
Similar Threads
help req. regarding parsing a file and tokenising the contents
Question about Arabic character
synchronizing a block of code
Split()-method
Has 5.0 and 6.0 gotten slower in this respect?