重新格式化 Java 字符串
我有一个看起来像这样的字符串:
CALDARI_STARSHIP_ENGINEERING
我需要将其编辑为看起来像
Caldari Starship Engineering
不幸的是现在是凌晨三点,我一生都无法弄清楚这一点。我在替换字符串中的内容时总是遇到麻烦,所以任何帮助都会很棒,并且会帮助我了解将来如何做到这一点。
I have a string that looks like this:
CALDARI_STARSHIP_ENGINEERING
and I need to edit it to look like
Caldari Starship Engineering
Unfortunately it's three in the morning and I cannot for the life of me figure this out. I've always had trouble with replacing stuff in strings so any help would be awesome and would help me understand how to do this in the future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
像这样的事情很简单:
在单词边界锚点上进行
分割
。另请参阅
匹配器
循环解决方案如果您不介意使用
StringBuffer
,您还可以使用Matcher.appendReplacement/Tail
循环,如下所示:正则表达式使用断言来匹配“尾部”部分一个单词,需要小写的部分。它查看
(?<=...)
后面,发现有一个单词边界\b
,后面跟着一个单词字符\w
。任何剩余的\w+
都需要匹配,以便可以小写。相关问题
\l
\u
、\L
和\U
。appendReplacement/Tail
只需要StringBuffer
Something like this is simple enough:
This
split
on the word boundary anchor.See also
Matcher
loop solutionIf you don't mind using
StringBuffer
, you can also useMatcher.appendReplacement/Tail
loop like this:The regex uses assertion to match the "tail" part of a word, the portion that needs to be lowercased. It looks behind
(?<=...)
to see that there's a word boundary\b
followed by a word character\w
. Any remaining\w+
would then need to be matched so it can be lowercased.Related questions
\l
\u
,\L
, and\U
.appendReplacement/Tail
only takesStringBuffer
您可以尝试以下操作:
WordUtils
是 Commons Lang 库的一部分 (http:// commons.apache.org/lang/)You can try this:
WordUtils
are part of the Commons Lang libraries (http://commons.apache.org/lang/)使用正则表达式:
Using reg-exps:
未经测试,但这就是我前段时间实现的方法:
请注意,EVE 许可协议可能禁止编写对您的职业生涯有帮助的外部工具。这可能会成为你学习 Python 的触发点,因为 EVE 的大部分都是用 Python 编写的:)。
Untested, but thats how I implemented the same some time ago:
Please note that the EVE license agreements might forbit writing external tools that help you in your careers. And it might be the trigger for you to learn Python, because most of EVE is written in Python :).
快速而肮脏的方法:
全部
小写拆分成单词:
然后循环遍历第一个字母大写的单词:
Quick and dirty way:
Lower case all
Split into words:
Then loop through words capitalising first letter: