如何将字符串中每个单词的第一个字符大写
Java中是否有一个内置函数可以将字符串中每个单词的第一个字符大写,并且不影响其他字符?
示例:
jon skeet
->乔恩·斯基特
迈尔斯·奥布莱恩
->Miles O'Brien
(B 仍为大写,这排除了标题大小写)old mcdonald
->Old Mcdonald
*
*(Old McDonald
也能找到,但我不认为它那么聪明。)
快速浏览一下 Java 字符串文档 仅显示 toUpperCase() 和
toLowerCase()
,这当然不能提供所需的行为。自然,谷歌的搜索结果主要由这两个功能主导。它看起来像是一个肯定已经被发明出来的轮子,所以问一下也没什么坏处,这样我将来就可以使用它。
Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others?
Examples:
jon skeet
->Jon Skeet
miles o'Brien
->Miles O'Brien
(B remains capital, this rules out Title Case)old mcdonald
->Old Mcdonald
*
*(Old McDonald
would be find too, but I don't expect it to be THAT smart.)
A quick look at the Java String Documentation reveals only toUpperCase()
and toLowerCase()
, which of course do not provide the desired behavior. Naturally, Google results are dominated by those two functions. It seems like a wheel that must have been invented already, so it couldn't hurt to ask so I can use it in the future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
WordUtils.capitalize(str)
(来自 apache commons- text)(注意:如果您需要将
"fOO BAr"
变为"Foo Bar"
,则使用capitalizeFully(..)
相反)WordUtils.capitalize(str)
(from apache commons-text)(Note: if you need
"fOO BAr"
to become"Foo Bar"
, then usecapitalizeFully(..)
instead)如果您只担心第一个单词的第一个字母大写:
If you're only worried about the first letter of the first word being capitalized:
以下方法将所有字母转换为大写/小写,具体取决于它们靠近空格或其他特殊字符的位置。
The following method converts all the letters into upper/lower case, depending on their position near a space or other special chars.
尝试这个非常简单的方法
示例给定字符串=“ram is good boy”
输出将是:Ram Is Good Boy
Try this very simple way
example givenString="ram is good boy"
Output will be: Ram Is Good Boy
我在 Java 8 中做了一个解决方案,恕我直言,它更具可读性。
该解决方案的要点可以在这里找到:https://gist.github.com/Hylke1982/166a792313c5e2df9d31
I made a solution in Java 8 that is IMHO more readable.
The Gist for this solution can be found here: https://gist.github.com/Hylke1982/166a792313c5e2df9d31
我编写了一个小类来将字符串中的所有单词大写。
可选的
多个分隔符
,每个分隔符都有其行为(之前、之后或两者都大写,以处理像O'Brian
这样的情况);可选的
区域设置
;不要破坏
代理对
。现场演示
输出:
I've written a small Class to capitalize all the words in a String.
Optional
multiple delimiters
, each one with its behavior (capitalize before, after, or both, to handle cases likeO'Brian
);Optional
Locale
;Don't breaks with
Surrogate Pairs
.LIVE DEMO
Output:
Note: first letter will always be capitalized (edit the source if you don't want that).
Please share your comments and help me to found bugs or to improve the code...
Code:
使用 org.apache.commons.lang.StringUtils 使其变得非常简单。
Using
org.apache.commons.lang.StringUtils
makes it very simple.从 Java 9+ 开始,
您可以使用
Matcher::replaceAll
像这样:示例:
输出
From Java 9+
you can use
Matcher::replaceAll
like this :Example :
Outputs
使用这个简单的代码:
结果: 你好
With this simple code:
Result: Hello
我正在使用以下功能。我认为它的性能更快。
I'm using the following function. I think it is faster in performance.
1. Java 8 Streams
示例:
对于
foo bAR
到Foo Bar
,将map()
方法替换为以下内容:2.
String.replaceAll()
(Java 9+)示例:
3. Apache Commons Text
For titlecase:
有关详细信息,请查看 本教程。
1. Java 8 Streams
Examples:
For
foo bAR
toFoo Bar
, replace themap()
method with the following:2.
String.replaceAll()
(Java 9+)Examples:
3. Apache Commons Text
For titlecase:
For details, checkout this tutorial.
使用 Split 方法将字符串拆分为单词,然后使用内置字符串函数将每个单词大写,然后附加在一起。
伪代码(ish)
最后的字符串看起来像这样
“你想要大写的句子”
Use the Split method to split your string into words, then use the built in string functions to capitalize each word, then append together.
Pseudo-code (ish)
In the end string looks something like
"The Sentence You Want To Apply Caps To"
如果您需要将标题大写,这可能很有用。它将由
" "
分隔的每个子字符串大写,但"a"
或"the"
等指定字符串除外。我还没跑,因为已经晚了,不过应该没问题。使用 Apache CommonsStringUtils.join()
在某一时刻。如果您愿意,可以用简单的循环替换它。This might be useful if you need to capitalize titles. It capitalizes each substring delimited by
" "
, except for specified strings such as"a"
or"the"
. I haven't ran it yet because it's late, should be fine though. Uses Apache CommonsStringUtils.join()
at one point. You can substitute it with a simple loop if you wish.在这里,我们追求完美的单词第一个字符大写
}
//输出:我的名字是 Ranjan
Here we go for perfect first char capitalization of word
}
//Output : My Name Is Ranjan
这是一个简单的函数
Here is a simple function
我决定添加另一种解决方案来将字符串中的单词大写:
功能:
调用示例:
I decided to add one more solution for capitalizing words in a string:
Function:
Example call:
Result:
使用:
Use:
有很多方法可以将第一个单词的第一个字母转换为大写。我有一个主意。这很简单:
There are many way to convert the first letter of the first word being capitalized. I have an idea. It's very simple:
这只是另一种方法:
This is just another way of doing it:
intiCap的可重用方法:
Reusable method for intiCap:
这是我的解决方案。
今晚我遇到了这个问题,决定搜索一下。我发现 Neelam Singh 的答案几乎就在那里,所以我决定解决这个问题(在空字符串上中断)并导致系统崩溃。
您正在寻找的方法名为
capString(String s)
,如下所示。它将“这里才凌晨 5 点”变成了“这里才凌晨 5 点”。
代码注释得很好,所以尽情享受吧。
Here is my solution.
I ran across this problem tonight and decided to search it. I found an answer by Neelam Singh that was almost there, so I decided to fix the issue (broke on empty strings) and caused a system crash.
The method you are looking for is named
capString(String s)
below.It turns "It's only 5am here" into "It's Only 5am Here".
The code is pretty well commented, so enjoy.
对于在 MVC 中使用 Velocity 的人,可以使用 StringUtils 类。
For those of you using Velocity in your MVC, you can use the
capitalizeFirstLetter()
method from the StringUtils class.如果你更喜欢番石榴...
If you prefer Guava...