Java 字符串转换为标题大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1086123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String conversion to Title Case
提问by
Is there any built in methods available to convert a string into Title Case format as such?
是否有任何内置方法可用于将字符串转换为 Title Case 格式?
回答by dfa
There are no capitalize() or titleCase() methods in Java's String class. You have two choices:
Java 的 String 类中没有 capitalize() 或 titleCase() 方法。你有两个选择:
- using commons lang string utils.
StringUtils.capitalize(null) = null
StringUtils.capitalize("") = ""
StringUtils.capitalize("cat") = "Cat"
StringUtils.capitalize("cAt") = "CAt"
StringUtils.capitalize("'cat'") = "'cat'"
- write (yet another) static helper method toTitleCase()
- 编写(又一个)静态辅助方法 toTitleCase()
Sample implementation
示例实现
public static String toTitleCase(String input) {
StringBuilder titleCase = new StringBuilder(input.length());
boolean nextTitleCase = true;
for (char c : input.toCharArray()) {
if (Character.isSpaceChar(c)) {
nextTitleCase = true;
} else if (nextTitleCase) {
c = Character.toTitleCase(c);
nextTitleCase = false;
}
titleCase.append(c);
}
return titleCase.toString();
}
Testcase
测试用例
System.out.println(toTitleCase("string"));
System.out.println(toTitleCase("another string"));
System.out.println(toTitleCase("YET ANOTHER STRING"));
outputs:
输出:
String Another String YET ANOTHER STRING
回答by aberrant80
Apache Commons StringUtils.capitalize()or Commons Text WordUtils.capitalize()
Apache Commons StringUtils.capitalize()或 Commons Text WordUtils.capitalize()
e.g: WordUtils.capitalize("i am FINE") = "I Am FINE"
from WordUtilsdoc
例如:WordUtils.capitalize("i am FINE") = "I Am FINE"
来自WordUtils文档
回答by scottb
If I may submit my take on the solution...
如果我可以提交我对解决方案的看法......
The following method is based on the one that dfa posted. It makes the following major change (which is suited to the solution I needed at the time): it forces all characters in the input string into lower case unless it is immediately preceded by an "actionable delimiter" in which case the character is coerced into upper case.
以下方法基于dfa发布的方法。它进行了以下主要更改(适合我当时需要的解决方案):它强制输入字符串中的所有字符都变成小写,除非它前面紧跟一个“可操作的分隔符”,在这种情况下,字符被强制转换为大写。
A major limitation of my routine is that it makes the assumption that "title case" is uniformly defined for all locales and is represented by the same case conventions I have used and so it is less useful than dfa's code in that respect.
我的例程的一个主要限制是它假设“标题大小写”是为所有语言环境统一定义的,并且由我使用的相同大小写约定表示,因此在这方面它不如 dfa 的代码有用。
public static String toDisplayCase(String s) {
final String ACTIONABLE_DELIMITERS = " '-/"; // these cause the character following
// to be capitalized
StringBuilder sb = new StringBuilder();
boolean capNext = true;
for (char c : s.toCharArray()) {
c = (capNext)
? Character.toUpperCase(c)
: Character.toLowerCase(c);
sb.append(c);
capNext = (ACTIONABLE_DELIMITERS.indexOf((int) c) >= 0); // explicit cast not needed
}
return sb.toString();
}
TEST VALUES
测试值
a string
maRTin o'maLLEY
john wilkes-booth
YET ANOTHER STRING
一个字符串
马丁奥马利
约翰威尔克斯布斯
又一串
OUTPUTS
输出
A String
Martin O'Malley
John Wilkes-Booth
Yet Another String
一个字符串
马丁·奥马利
约翰·威尔克斯-布斯
另一个字符串
回答by jiehanzheng
Use WordUtils.capitalizeFully()from Apache Commons.
使用Apache Commons 中的WordUtils.capitalizeFully()。
WordUtils.capitalizeFully(null) = null
WordUtils.capitalizeFully("") = ""
WordUtils.capitalizeFully("i am FINE") = "I Am Fine"
回答by Vishwanath Dasa
Sorry I am a beginner so my coding habit sucks!
对不起,我是初学者,所以我的编码习惯很糟糕!
public class TitleCase {
String title(String sent)
{
sent =sent.trim();
sent = sent.toLowerCase();
String[] str1=new String[sent.length()];
for(int k=0;k<=str1.length-1;k++){
str1[k]=sent.charAt(k)+"";
}
for(int i=0;i<=sent.length()-1;i++){
if(i==0){
String s= sent.charAt(i)+"";
str1[i]=s.toUpperCase();
}
if(str1[i].equals(" ")){
String s= sent.charAt(i+1)+"";
str1[i+1]=s.toUpperCase();
}
System.out.print(str1[i]);
}
return "";
}
public static void main(String[] args) {
TitleCase a = new TitleCase();
System.out.println(a.title(" enter your Statement!"));
}
}
回答by JoeG
The simplest way of converting any string into a title case, is to use googles package org.apache.commons.lang.WordUtils
将任何字符串转换为标题大小写的最简单方法是使用 googles 包 org.apache.commons.lang.WordUtils
System.out.println(WordUtils.capitalizeFully("tHis will BE MY EXAMple"));
Will result this
会导致这个
This Will Be My Example
这将是我的榜样
I'm not sure why its named "capitalizeFully", where in fact the function is not doing a full capital result, but anyways, thats the tool that we need.
我不知道为什么它被命名为“capitalizeFully”,实际上该函数并没有做一个完整的资本结果,但无论如何,这就是我们需要的工具。
回答by Vegegoku
You can use apache commons langs like this :
您可以像这样使用 apache commons langs:
WordUtils.capitalizeFully("this is a text to be capitalize")
you can find the java doc here : WordUtils.capitalizeFully java doc
你可以在这里找到java文档: WordUtils.capitalizeFully java doc
and if you want to remove the spaces in between the worlds you can use :
如果你想删除世界之间的空间,你可以使用:
StringUtils.remove(WordUtils.capitalizeFully("this is a text to be capitalize")," ")
you can find the java doc for String StringUtils.remove java doc
你可以找到 String StringUtils.remove java doc 的 java doc
i hope this help.
我希望这有帮助。
回答by Manish Bansal
I know this is older one, but doesn't carry the simple answer, I needed this method for my coding so I added here, simple to use.
我知道这是旧的,但没有简单的答案,我的编码需要这种方法,所以我在这里添加,使用简单。
public static String toTitleCase(String input) {
input = input.toLowerCase();
char c = input.charAt(0);
String s = new String("" + c);
String f = s.toUpperCase();
return f + input.substring(1);
}
回答by gkarthiks
you can very well use
你可以很好地使用
org.apache.commons.lang.WordUtils
org.apache.commons.lang.WordUtils
or
或者
CaseFormat
案例格式
from Google's API.
来自谷歌的 API。
回答by user1743960
This is something I wrote to convert snake_case to lowerCamelCase but could easily be adjusted based on the requirements
这是我写的将snake_case转换为lowerCamelCase但可以根据要求轻松调整的内容
private String convertToLowerCamel(String startingText)
{
String[] parts = startingText.split("_");
return parts[0].toLowerCase() + Arrays.stream(parts)
.skip(1)
.map(part -> part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase())
.collect(Collectors.joining());
}