Java字符串转换(例如hello world – > Hello World)

Java String transform (e.g. hello world --> Hello World)

本问题已经有最佳答案,请猛点这里访问。

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

编写进行以下字符串转换的函数最简单的方法是什么;下面是一些示例:

1
2
 - hello --> Hello
 - hello world --> Hello World

我只是在数据库中有一些标签,我想确保有一致的情况;我当然知道这可以在传统意义上实现,但我只是想知道是否有更方便/可靠的功能可以帮助实现这一点。


您可以在apache commons lang类wordutils(org.apache.commons.lang中使用capitalizecapitalizeFully

1
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).

1
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.


如果要将第一个字符大写,可以查看apache common utils lang wordutils