0

Possible Duplicate:
Capitalize First Char of Each Word in a String Java

What would be the easiest way to write a function that makes the following String transformations; here are some examples:

 - hello --> Hello
 - hello world --> Hello World

I just have some tags in the Db, and I wan to ensure that have consistent case; I know of course this could be done in the traditional sense, but I'm just interested to know if there are more convenient/reliable functions that could help to accomplish this.

Community
  • 1
  • 1
Larry
  • 11,439
  • 15
  • 61
  • 84
  • 1
    At what step exactly are you stucking while implementing this? At splitting the string on space into parts, or at uppercasing the first letter of every part, or at glueing the parts together? – BalusC Oct 24 '11 at 16:58
  • @BalusC, surely is not hard to implement, but since it's already done... as Larry says, laziness is one of the great virtues of programmers :-) – stivlo Oct 24 '11 at 17:06
  • @stivlo: Exactly, well said! hehe – Larry Oct 24 '11 at 17:09

2 Answers2

5

You can use capitalize or capitalizeFully in Apache Commons Lang class WordUtils (org.apache.commons.lang).

public static java.lang.String capitalize(java.lang.String str)

Capitalizes all the whitespace separated words in a String. Only the first letter of each word is changed. To convert the rest of each word to lowercase at the same time, use capitalizeFully(String).

public static java.lang.String capitalizeFully(java.lang.String str)

Converts all the whitespace separated words in a String into capitalized words, that is each word is made up of a titlecase character and then a series of lowercase characters.

stivlo
  • 83,644
  • 31
  • 142
  • 199
  • sorry, I never used capitalizeFully. And I am not sure if I understand the javadoc. What is the difference? (just interested) – MarianP Oct 24 '11 at 17:16
  • 1
    capitalize("hEllO WorlD") -> "HEllO WorlD", capitalizeFully("hEllO WorlD") -> "Hello World" – stivlo Oct 24 '11 at 17:25
  • that's what I suspected, just the javadoc did not make it clear for me without much thinking. thanks a lot. – MarianP Oct 24 '11 at 18:25
1

If you want to capitalize first character, you can look in to apache common utils lang WordUtils

Jayendra
  • 52,349
  • 4
  • 80
  • 90