6

In php, the method ucwords converts any string in a string where each words first character is in uppercase, and all other characters are lower case.

I always end up making my own implementation, and I'm wondering if a standard method exists.

Charles
  • 50,943
  • 13
  • 104
  • 142
XGouchet
  • 10,002
  • 10
  • 48
  • 83
  • 2
    http://stackoverflow.com/questions/1149855/how-to-upper-case-every-first-letter-of-word-in-a-string – Jarrod Nettles Feb 28 '11 at 15:32
  • @Jarrod -- the link's broken. The Apache StringUtils follows WordUtils algo. As mentioned in JavaDoc – Nishant Feb 28 '11 at 15:35
  • @Nishant: the link works for me... – posdef Feb 28 '11 at 15:37
  • @posdef ooh. Did not for me. In fact, I have just updated with the correct link in that answer. BTW, I was talking about the WordUtil's link given in the answer, not the link to the answer. – Nishant Feb 28 '11 at 15:39
  • 1
    The php call `ucwords` is short hand for `upper case words`. The process of converting a lower case word to an upper case word is typically called `capitalization`. – Edwin Buck Feb 28 '11 at 15:52
  • Please note that capitalization could be complicated for some Unicode characters. There are upper-case, lower-case and title-case in some languages. Please refer to ICU page http://userguide.icu-project.org/transforms/casemappings for details – Tommy Siu Feb 28 '11 at 16:05
  • @Tommy Siu StringUtils uses [TitleCase](http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Character.html#TITLECASE_LETTER) to circumvent the Unicode char issue. – Nishant Feb 28 '11 at 17:09

3 Answers3

8

That's called capitalization. Use Apache Commons's StringUtils to do that.

See more here:

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

WordUtils is also worth looking at. See here

Jon
  • 365
  • 2
  • 13
Nishant
  • 54,584
  • 13
  • 112
  • 127
  • Ok thanks for the link. I won't be able to use it much though, as I'm working on Android (which does not include the apache org.apache.commons.lang library). – XGouchet Feb 28 '11 at 15:44
  • @XGouchet That's weird. You **can** use external Jar in Android project. see here http://stackoverflow.com/questions/1334802/how-can-i-use-external-jars-in-an-android-project – Nishant Feb 28 '11 at 15:47
  • @XGouchet please tag your question as android – Nishant Feb 28 '11 at 15:48
  • 4
    Yes, I know I can but I want to keep my apps small as possible and integrating a jar just for this small feature is not a good idea. – XGouchet Jun 12 '13 at 06:43
5

Otherwise it's a rather simple fix such as; String string1 = someString.substring(0,1).toUpperCase() + someString.substring(1);

You can put it in a function, and call it whenever you need. Saves you the trouble of maintaining libraries you don't need. (not that apache commons is ever trouble, but you get the point..)

EDIT: someString.substring(1) part can be written as someString.substring(1).toLowerCase() just to make sure that the rest of the string is in lowercase

posdef
  • 6,498
  • 11
  • 46
  • 94
4

I don't know about any direct equivalent, but you can always write one:

public static String capitalize(String input) {
   if (input == null || input.length() <= 0) {
     return input;
   }
   char[] chars = new char[1];
   input.getChars(0, 1, chars, 0);
   if (Character.isUpperCase(chars[0])) {
     return input;
   } else {
     StringBuilder buffer = new StringBuilder(input.length());
     buffer.append(Character.toUpperCase(chars[0]));
     buffer.append(input.toCharArray(), 1, input.length()-1);
     return buffer.toString();
   }
}
scravy
  • 11,904
  • 14
  • 72
  • 127
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138