如何在Java中将字符串的第一个字母大写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3904579/
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
How to capitalize the first letter of a String in Java?
提问by sumithra
I am using Java to get a String
input from the user. I am trying to make the first letter of this input capitalized.
我正在使用 JavaString
从用户那里获取输入。我正在尝试将此输入的第一个字母大写。
I tried this:
我试过这个:
String name;
BufferedReader br = new InputStreamReader(System.in);
String s1 = name.charAt(0).toUppercase());
System.out.println(s1 + name.substring(1));
which led to these compiler errors:
这导致了这些编译器错误:
Type mismatch: cannot convert from InputStreamReader to BufferedReader
Cannot invoke toUppercase() on the primitive type char
类型不匹配:无法从 InputStreamReader 转换为 BufferedReader
无法在原始类型 char 上调用 toUppercase()
回答by Rekin
String str = "java";
String cap = str.substring(0, 1).toUpperCase() + str.substring(1);
// cap = "Java"
With your example:
以你的例子:
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Actually use the Reader
String name = br.readLine();
// Don't mistake String object with a Character object
String s1 = name.substring(0, 1).toUpperCase();
String nameCapitalized = s1 + name.substring(1);
System.out.println(nameCapitalized);
}
回答by Zaki
回答by Suresh Kumar
回答by Grodriguez
What you want to do is probably this:
你想要做的可能是这样的:
s1 = name.substring(0, 1).toUpperCase() + name.substring(1);
(converts first char to uppercase and adds the remainder of the original string)
(将第一个字符转换为大写并添加原始字符串的其余部分)
Also, you create an input stream reader, but never read any line. Thus name
will always be null
.
此外,您创建了一个输入流读取器,但从不读取任何行。因此,name
将永远是null
。
This should work:
这应该有效:
BufferedReader br = new InputstreamReader(System.in);
String name = br.readLine();
String s1 = name.substring(0, 1).toUpperCase() + name.substring(1);
回答by jerjer
You can also try this:
你也可以试试这个:
String s1 = br.readLine();
char[] chars = s1.toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
s1= new String(chars);
System.out.println(s1);
This is better(optimized) than with using substring. (but not to worry on small string)
这比使用子字符串更好(优化)。(但不要担心小字符串)
回答by Adeel Ansari
This is just to show you, that you were not that wrong.
这只是为了告诉你,你没有错。
BufferedReader br = new InputstreamReader(System.in);
// Assuming name is not blank
String name = br.readLine();
//No more error telling that you cant convert char to string
String s1 = (""+name.charAt(0)).toUppercase());
// Or, as Carlos prefers. See the comments to this post.
String s1 = Character.toString(name.charAt(0)).toUppercase());
System.out.println(s1+name.substring(1));
Note:This is not at all the best way to do it. This is just to show the OP that it can be done using charAt()
as well. ;)
注意:这根本不是最好的方法。这只是为了向 OP 展示它也可以使用charAt()
。;)
回答by JDJ
You can use substring()
to do this.
您可以使用它substring()
来执行此操作。
But there are two different cases:
但是有两种不同的情况:
Case 1
情况1
If the String
you are capitalizing is meant to be human-readable, you should also specify the default locale:
如果String
您要大写是为了便于人类阅读,您还应该指定默认语言环境:
String firstLetterCapitalized =
myString.substring(0, 1).toUpperCase(Locale.getDefault()) + myString.substring(1);
Case 2
案例二
If the String
you are capitalizing is meant to be machine-readable, avoid using Locale.getDefault()
because the string that is returned will be inconsistent across different regions, and in this case always specify the same locale (for example, toUpperCase(Locale.ENGLISH)
). This will ensure that the strings you are using for internal processing are consistent, which will help you avoid difficult-to-find bugs.
如果String
您要大写的 是机器可读的,请避免使用,Locale.getDefault()
因为返回的字符串在不同区域之间会不一致,并且在这种情况下始终指定相同的区域设置(例如,toUpperCase(Locale.ENGLISH)
)。这将确保您用于内部处理的字符串是一致的,这将帮助您避免难以发现的错误。
Note: You do not have to specify Locale.getDefault()
for toLowerCase()
, as this is done automatically.
注意:您不必指定Locale.getDefault()
for toLowerCase()
,因为这是自动完成的。
回答by Jorgesys
The shorter/faster version code to capitalize the first letter of a String is:
将字符串的第一个字母大写的更短/更快的版本代码是:
String name = "stackoverflow";
name = name.substring(0,1).toUpperCase() + name.substring(1).toLowerCase();
the value of name
is "Stackoverflow"
的价值name
是"Stackoverflow"
回答by Ameen Maheen
try this one
试试这个
What this method does is that, Consider the word "hello world" this method turn it into "Hello World" capitalize the beginning of each word .
这个方法的作用是,考虑单词“hello world”这个方法把它变成“Hello World”,每个单词的开头都大写。
private String capitalizer(String word){
String[] words = word.split(" ");
StringBuilder sb = new StringBuilder();
if (words[0].length() > 0) {
sb.append(Character.toUpperCase(words[0].charAt(0)) + words[0].subSequence(1, words[0].length()).toString().toLowerCase());
for (int i = 1; i < words.length; i++) {
sb.append(" ");
sb.append(Character.toUpperCase(words[i].charAt(0)) + words[i].subSequence(1, words[i].length()).toString().toLowerCase());
}
}
return sb.toString();
}