sum*_*hra 266 java string capitalize
我正在使用Java来获取String用户的输入.我试图使这个输入的第一个字母大写.
我试过这个:
String name;
BufferedReader br = new InputStreamReader(System.in);
String s1 = name.charAt(0).toUppercase());
System.out.println(s1 + name.substring(1));
Run Code Online (Sandbox Code Playgroud)
这导致了这些编译器错误:
类型不匹配:无法从InputStreamReader转换为BufferedReader
无法在基本类型char上调用toUppercase()
Rek*_*kin 354
String str = "java";
String cap = str.substring(0, 1).toUpperCase() + str.substring(1);
// cap = "Java"
Run Code Online (Sandbox Code Playgroud)
用你的例子:
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);
}
Run Code Online (Sandbox Code Playgroud)
Boz*_*zho 191
StringUtils.capitalize(..) 来自commons-lang
Jor*_*sys 80
String name = "stackoverflow";
name = name.substring(0,1).toUpperCase() + name.substring(1).toLowerCase();
Run Code Online (Sandbox Code Playgroud)
价值name是"Stackoverflow"
Fan*_*ing 46
步骤1:
通过将它放在build.gradle依赖项中来导入apache的常用lang库
compile 'org.apache.commons:commons-lang3:3.6'
Run Code Online (Sandbox Code Playgroud)
第2步:
如果您确定您的字符串全部为小写,或者您只需要初始化第一个字母,请直接调用
StringUtils.capitalize(yourString);
Run Code Online (Sandbox Code Playgroud)
如果你想确保只有第一个字母大写,比如为第一个字母大写,请先enum调用,toLowerCase()并记住NullPointerException如果输入字符串为null ,它将抛出.
StringUtils.capitalize(YourEnum.STUFF.name().toLowerCase());
StringUtils.capitalize(yourString.toLowerCase());
Run Code Online (Sandbox Code Playgroud)
以下是apache提供的更多示例.这是免费的例外
StringUtils.capitalize(null) = null
StringUtils.capitalize("") = ""
StringUtils.capitalize("cat") = "Cat"
StringUtils.capitalize("cAt") = "CAt"
StringUtils.capitalize("'cat'") = "'cat'"
Run Code Online (Sandbox Code Playgroud)
注意:
WordUtils也包含在此库中,但已弃用.请不要使用它.
Gro*_*uez 22
你想做的可能是这样的:
s1 = name.substring(0, 1).toUpperCase() + name.substring(1);
Run Code Online (Sandbox Code Playgroud)
(将第一个char转换为大写并添加原始字符串的其余部分)
此外,您创建一个输入流阅读器,但从不读取任何行.因此name永远如此null.
这应该工作:
BufferedReader br = new InputstreamReader(System.in);
String name = br.readLine();
String s1 = name.substring(0, 1).toUpperCase() + name.substring(1);
Run Code Online (Sandbox Code Playgroud)
epo*_*pox 21
import static org.springframework.util.StringUtils.capitalize;
...
return capitalize(name);
Run Code Online (Sandbox Code Playgroud)
实施: org/springframework/util/StringUtils.java#L535-L555
参考: javadoc-api/org/springframework/util/StringUtils.html#capitalize
注意:如果您已经有 Apache Common Lang 依赖项,那么请考虑使用他们的 StringUtils.capitalize 作为其他答案的建议。
Ami*_*emi 15
Java:
只是一个大写每个字符串的辅助方法。
public static String capitalize(String str)
{
if(str == null) return str;
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
Run Code Online (Sandbox Code Playgroud)
之后,只需致电 str = capitalize(str)
科特林:
str.capitalize()
Run Code Online (Sandbox Code Playgroud)
Zak*_*aki 14
WordUtils.capitalize(java.lang.String)来自Apache Commons.
小智 12
以下解决方案将有效.
String A = "stackOverflow";
String ACaps = A.toUpperCase().charAt(0)+A.substring(1,A.length());
//Will print StackOverflow
Run Code Online (Sandbox Code Playgroud)
你不能在原始字符上使用toUpperCase(),但你可以先将整个String设置为大写,然后取第一个字符,然后附加到子字符串,如上所示.
小智 7
使用此实用方法获取所有首字母大写。
String captializeAllFirstLetter(String name)
{
char[] array = name.toCharArray();
array[0] = Character.toUpperCase(array[0]);
for (int i = 1; i < array.length; i++) {
if (Character.isWhitespace(array[i - 1])) {
array[i] = Character.toUpperCase(array[i]);
}
}
return new String(array);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
它将工作 101%
public class UpperCase {
public static void main(String [] args) {
String name;
System.out.print("INPUT: ");
Scanner scan = new Scanner(System.in);
name = scan.next();
String upperCase = name.substring(0, 1).toUpperCase() + name.substring(1);
System.out.println("OUTPUT: " + upperCase);
}
}
Run Code Online (Sandbox Code Playgroud)
在 Android Studio 中
将此依赖项添加到您的 build.gradle (Module: app)
dependencies {
...
compile 'org.apache.commons:commons-lang3:3.1'
...
}
Run Code Online (Sandbox Code Playgroud)
现在你可以使用
String string = "STRING WITH ALL CAPPS AND SPACES";
string = string.toLowerCase(); // Make all lowercase if you have caps
someTextView.setText(WordUtils.capitalize(string));
Run Code Online (Sandbox Code Playgroud)
小智 6
将字符串设置为小写,然后将第一个Letter设置为upper,如下所示:
userName = userName.toLowerCase();
Run Code Online (Sandbox Code Playgroud)
然后把第一个字母大写:
userName = userName.substring(0, 1).toUpperCase() + userName.substring(1).toLowerCase();
Run Code Online (Sandbox Code Playgroud)
substring只是得到一个更大的字符串,然后我们将它们组合在一起.
那WordUtils.capitalizeFully()呢?
import org.apache.commons.lang3.text.WordUtils;
public class Main {
public static void main(String[] args) {
final String str1 = "HELLO WORLD";
System.out.println(capitalizeFirstLetter(str1)); // output: Hello World
final String str2 = "Hello WORLD";
System.out.println(capitalizeFirstLetter(str2)); // output: Hello World
final String str3 = "hello world";
System.out.println(capitalizeFirstLetter(str3)); // output: Hello World
final String str4 = "heLLo wORld";
System.out.println(capitalizeFirstLetter(str4)); // output: Hello World
}
private static String capitalizeFirstLetter(String str) {
return WordUtils.capitalizeFully(str);
}
}
Run Code Online (Sandbox Code Playgroud)
最短的:
String message = "my message";
message = Character.toUpperCase(message.charAt(0)) + message.substring(1);
System.out.println(message) // Will output: My message
Run Code Online (Sandbox Code Playgroud)
为我工作.
简单的解决方案!不需要任何外部库,它可以处理空字符串或一个字母字符串。
private String capitalizeFirstLetter(@NonNull String str){
return str.length() == 0 ? str
: str.length() == 1 ? str.toUpperCase()
: str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
}
Run Code Online (Sandbox Code Playgroud)
你也可以尝试这个:
String s1 = br.readLine();
char[] chars = s1.toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
s1= new String(chars);
System.out.println(s1);
Run Code Online (Sandbox Code Playgroud)
这比使用子字符串更好(优化)。(但不用担心小绳子)
您可以使用它substring()来执行此操作。
但是有两种不同的情况:
情况1
如果String您要大写是为了便于人类阅读,您还应该指定默认语言环境:
String firstLetterCapitalized =
myString.substring(0, 1).toUpperCase(Locale.getDefault()) + myString.substring(1);
Run Code Online (Sandbox Code Playgroud)
案例二
如果String您要大写的 是机器可读的,请避免使用,Locale.getDefault()因为返回的字符串在不同区域之间会不一致,并且在这种情况下始终指定相同的区域设置(例如,toUpperCase(Locale.ENGLISH))。这将确保您用于内部处理的字符串是一致的,这将帮助您避免难以发现的错误。
注意:您不必指定Locale.getDefault()for toLowerCase(),因为这是自动完成的。
String str1 = "hello";
str1.substring(0, 1).toUpperCase()+str1.substring(1);
Run Code Online (Sandbox Code Playgroud)
这是我关于所有可能选项的主题的详细文章在 Android 中大写字符串的首字母
Java中字符串首字母大写的方法
public static String capitalizeString(String str) {
String retStr = str;
try { // We can face index out of bound exception if the string is null
retStr = str.substring(0, 1).toUpperCase() + str.substring(1);
}catch (Exception e){}
return retStr;
}
Run Code Online (Sandbox Code Playgroud)
KOTLIN中字符串首字母大写的方法
fun capitalizeString(str: String): String {
var retStr = str
try { // We can face index out of bound exception if the string is null
retStr = str.substring(0, 1).toUpperCase() + str.substring(1)
} catch (e: Exception) {
}
return retStr
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
467199 次 |
| 最近记录: |