Java中如何将字符串的首字母大写?
我正在使用 Java 从用户获取 String
输入。我正在尝试将此输入的第一个字母大写。
我尝试了这个:
String name;
BufferedReader br = new InputStreamReader(System.in);
String s1 = name.charAt(0).toUppercase());
System.out.println(s1 + name.substring(1));
这导致了这些编译器错误:
- <块引用>
类型不匹配:无法从 InputStreamReader 转换为 BufferedReader
- <块引用>
无法对基本类型 char 调用 toUppercase()
I am using Java to get a String
input from the user. I am trying to make the first letter of this input capitalized.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
以你的例子:
With your example:
StringUtils.capitalize(..)
来自 commons-语言StringUtils.capitalize(..)
from commons-lang将字符串的第一个字母大写的更短/更快的版本代码是:
name
的值是"Stackoverflow"
The shorter/faster version code to capitalize the first letter of a String is:
the value of
name
is"Stackoverflow"
使用 Apache 的公共库。将你的大脑从这些东西中解放出来,避免空指针和空指针。索引越界异常
第 1 步:
通过将其放入
build.gradle
依赖项中来导入 apache 的通用语言库第 2 步:
如果您确定你的字符串全部是小写,或者你只需要初始化第一个字母,直接调用
如果你想确保只有第一个字母大写,就像对
enum
这样做,调用< code>toLowerCase() 首先,请记住,如果输入字符串为 null,它将抛出NullPointerException
。这里有 apache 提供的更多示例。它是免费的
注意:
WordUtils
也包含在此库中,但已弃用。请不要使用它。Use Apache's common library. Free your brain from these stuffs and avoid Null Pointer & Index Out Of Bound Exceptions
Step 1:
Import apache's common lang library by putting this in
build.gradle
dependenciesStep 2:
If you are sure that your string is all lower case, or all you need is to initialize the first letter, directly call
If you want to make sure that only the first letter is capitalized, like doing this for an
enum
, calltoLowerCase()
first and keep in mind that it will throwNullPointerException
if the input string is null.Here are more samples provided by apache. it's exception free
Note:
WordUtils
is also included in this library, but is deprecated. Please do not use that.如果您使用SPRING:
实现: org/springframework/util/StringUtils.java#L535-L555
参考: javadoc-api/org/springframework/util/StringUtils.html#capitalize
注意: 如果您已经有 Apache Common Lang 依赖项,请考虑使用它们的 StringUtils.capitalize 作为其他答案表明。
if you use SPRING:
IMPLEMENTATION: org/springframework/util/StringUtils.java#L535-L555
REF: javadoc-api/org/springframework/util/StringUtils.html#capitalize
NOTE: If you already have Apache Common Lang dependency, then consider using their StringUtils.capitalize as other answers suggest.
Java:
只是一个用于将每个字符串大写的辅助方法。
之后只需调用
str = Capitalize(str)
Kotlin:
Java:
simply a helper method for capitalizing every string.
After that simply call
str = capitalize(str)
Kotlin:
您想要做的可能是这样的:(
将第一个字符转换为大写并添加原始字符串的其余部分)
此外,您创建一个输入流读取器,但从不读取任何行。因此
name
将始终为null
。这应该有效:
What you want to do is probably this:
(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 benull
.This should work:
WordUtils.capitalize(java.lang.String)
来自 阿帕奇共享。WordUtils.capitalize(java.lang.String)
from Apache Commons.下面的解决方案将起作用。
您不能在原始 char 上使用 toUpperCase() ,但您可以先将整个 String 转换为大写,然后取出第一个字符,然后附加到子字符串,如上所示。
Below solution will work.
You can't use toUpperCase() on primitive char , but you can make entire String to Uppercase first then take the first char, then to append to the substring as shown above.
使用此实用方法将每个单词的第一个字母大写。
Use this utility method to capitalize the first letter of every word.
使用
WordUtils.capitalize(str)
。Use
WordUtils.capitalize(str)
.将字符串设置为小写,然后将第一个字母设置为大写,如下所示:
然后将第一个字母大写:
子字符串只是获取较大字符串的一部分,然后我们将它们组合在一起。
Set the string to lower case, then set the first Letter to upper like this:
then to capitalise the first letter:
substring is just getting a piece of a larger string, then we are combining them back together.
也是最短的:
为我工作。
Shortest too:
Worked for me.
这是我关于所有可能选项的主题的详细文章 Android中字符串首字母大写
Java中字符串首字母大写的方法
KOTLIN中字符串首字母大写的方法
Here is my detailed article on the topic for all possible options Capitalize First Letter of String in Android
Method to Capitalize First Letter of String in Java
Method to Capitalize First Letter of String in KOTLIN
101% 有效
IT WILL WORK 101%
在 Android Studio 中
将此依赖项添加到您的
build.gradle (Module: app)
现在您可以使用
In Android Studio
Add this dependency to your
build.gradle (Module: app)
Now you can use
怎么样 WordUtils.capitalizeFully()?
What about WordUtils.capitalizeFully()?
简单的解决方案!不需要任何外部库,它可以处理空字符串或一个字母字符串。
Simple solution! doesn't require any external library, it can handle empty or one letter string.
您也可以尝试这个:
这比使用子字符串更好(优化)。 (但不用担心小绳子)
You can also try this:
This is better (optimized) than with using substring. (but not to worry on small string)
您可以使用
substring()
来执行此操作。但有两种不同的情况:
情况 1
如果您要大写的
字符串
是为了便于人类阅读,您还应该指定默认区域设置:情况 2
如果您要大写的
String
是为了机器可读,请避免使用Locale.getDefault()
,因为返回的字符串在不同的环境中会不一致。区域,并且在这种情况下始终指定相同的区域设置(例如,toUpperCase(Locale.ENGLISH)
)。这将确保您用于内部处理的字符串是一致的,这将帮助您避免难以发现的错误。注意:您不必为
toLowerCase()
指定Locale.getDefault()
,因为这是自动完成的。You can use
substring()
to do this.But there are two different cases:
Case 1
If the
String
you are capitalizing is meant to be human-readable, you should also specify the default locale:Case 2
If the
String
you are capitalizing is meant to be machine-readable, avoid usingLocale.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.Note: You do not have to specify
Locale.getDefault()
fortoLowerCase()
, as this is done automatically.如果输入是 UpperCase ,则使用以下内容:
str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
如果输入为 LowerCase ,则使用以下内容:
str.substring(0, 1).toUpperCase() + str.substring(1);
If Input is UpperCase ,then Use following :
str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
If Input is LowerCase ,then Use following :
str.substring(0, 1).toUpperCase() + str.substring(1);
现有的答案要么
char
是一个单独的字符(代码点),而它是一个 UTF-16 单词,可以是代理对的一半,或者让我们看一下代理字符(每个这样的字符由两个UTF-16 单词 — Java
char
s),并且可以有大写和小写变体:其中许多对您来说可能看起来像“豆腐”(□),但它们大多是罕见脚本和某些字体的有效字符支持他们。
例如,让我们看一下 Deseret Small Letter Long I (
Existing answers are either
char
is a separate character (code point), while it is a UTF-16 word which can be a half of a surrogate pair, orLet's look at surrogate characters (every such character consist of two UTF-16 words — Java
char
s) and can have upper and lowercase variants:Many of them may look like 'tofu' (□) for you but they are mostly valid characters of rare scripts and some typefaces support them.
For example, let's look at Deseret Small Letter Long I (????), U+10428,
"\uD801\uDC28"
:So, a code point can be capitalized even in cases when
char
cannot be.Considering this, let's write a correct (and Java 1.5 compatible!) capitalizer:
And check whether it works:
See also:
当前的答案要么不正确,要么使这个简单的任务过于复杂。经过一些研究后,我提出了两种方法与:
1。 String 的
substring()
方法示例:
2. Apache Commons Lang
Apache Commons Lang 库为此目的提供了
StringUtils
类:不要忘记将以下依赖项添加到您的
pom.xml
文件中:Current answers are either incorrect or over-complicate this simple task. After doing some research, here are two approaches I come up with:
1. String's
substring()
MethodExamples:
2. Apache Commons Lang
The Apache Commons Lang library provides
StringUtils
the class for this purpose:Don't forget to add the following dependency to your
pom.xml
file:试试这个
这个方法的作用是,考虑单词“hello world”,这个方法将其转换为“Hello World”,每个单词的开头大写。
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 .
这只是为了向你表明,你并没有错。
注意: 这根本不是最好的方法。这只是为了向 OP 表明它也可以使用 charAt() 来完成。 ;)
This is just to show you, that you were not that wrong.
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. ;)这会起作用
This will work
您可以使用以下代码:
You can use the following code:
使用commons.lang.StringUtils 最好的答案是:
我发现它很棒,因为它用 StringBuffer 包装了字符串。您可以根据需要并使用相同的实例来操作 StringBuffer。
Using
commons.lang.StringUtils
the best answer is:I find it brilliant since it wraps the string with a StringBuffer. You can manipulate the StringBuffer as you wish and though using the same instance.
查看 ACL WordUtils。
WordUtils.capitalize("你的字符串") == "你的字符串"
如何将字符串中单词的每个第一个字母大写?
Have a look at ACL WordUtils.
WordUtils.capitalize("your string") == "Your String"
How to upper case every first letter of word in a string?