• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

splitting a StringBuffer into tokens?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fortunately, there is a StringTokenizer class which will fulfil your needs very easily.

Ken
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any "split" method in the String class - where did that come from?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic