This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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 ]
[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.
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
Joined: Nov 11, 2003
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.
Joined: Jun 04, 2004
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