• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Converting a String to uppercase?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya All

If I have a String such as String temp = "this is an example" How do I convert the start of each of the words within the string to uppercase so the output would be "This Is An Example"?

Any help would be greatfull

Cheers
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you capitalize the first letter and then read through the string and each time you find a blank space, capitolize the NEXT letter
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know of any existing methods for this, so I guess I would index through each char in the String and check to see if it's a space or lowercase char. Finding a space would set a flag allowing the next character to be converted to uppercase if needed. (This should initially be set to true in order to allow the first letter to be capitalized.)

Note that chars can be treated as numeric values (although you'll need to cast back to type char after performing arithmetic on them). Uppercase letters A-Z are represented by numeric values 65-90, and lowercase (a-z) are represented by 97-122. So if you have a lowercase value, you can convert it to uppercase by subtracting 32. (Ensure first that the char you have is, in fact, within that lowercase range.) A space is represented by 32.

Ref: http://www.lookuptables.com/

To avoid creating numerous String objects, you might want to use a StringBuffer (or StringBuilder in Java 1.5).
[ April 06, 2005: Message edited by: marc weber ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider using a StringTokenizer to break your String based on the � � (empty space) and capitalize the first letter of each token.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...or the String.split() method (1.4+ ), instead of a StringTokenizer.
 
Jamie Cotton
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya thankyou all for your input i have now managed to do it

 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic