我使用Java从用户获得一个字符串输入。我试着让输入的第一个字母大写。

我试了一下:

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()


当前回答

当前的答案要么是不正确的,要么是把这个简单的任务过于复杂了。在做了一些研究之后,我想到了以下两种方法:

1. 字符串的substring()方法

public static String capitalize(String str) {
    if(str== null || str.isEmpty()) {
        return str;
    }

    return str.substring(0, 1).toUpperCase() + str.substring(1);
}

例子:

System.out.println(capitalize("java")); // Java
System.out.println(capitalize("beTa")); // BeTa
System.out.println(capitalize(null)); // null

2. Apache Commons Lang

Apache Commons Lang库为此目的提供了StringUtils类:

System.out.println(StringUtils.capitalize("apache commons")); // Apache commons
System.out.println(StringUtils.capitalize("heLLO")); // HeLLO
System.out.println(StringUtils.uncapitalize(null)); // null

不要忘记在pom.xml文件中添加以下依赖项:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

其他回答

如果Input是大写,那么使用以下语句:

str.substring(0,1).toUpperCase() + str.substring(1).toLowerCase();

如果输入是小写的,那么使用以下:

str.substring(0, 1).toUpperCase() + str.substring(1);

一种方法。

String input = "someТекст$T%$4čřЭ"; //Enter your text.
if (input == null || input.isEmpty()) {
    return "";
}

char [] chars = input.toCharArray();
chars[0] = chars[0].toUpperCase();
String res = new String(chars);
return res;

此方法的缺点是,如果inputString很长,则将有三个这样长度的对象。和你一样

String s1 = input.substring(1).toUpperCase();
String s2 = input.substring(1, lenght);
String res = s1 + s2;

甚至

//check if not null.
StringBuilder buf = new StringBuilder(input);
char ch = buf.getCharAt(0).toUpperCase();
buf.setCharAt(0, ch);
return buf.toString();

当前的答案要么是不正确的,要么是把这个简单的任务过于复杂了。在做了一些研究之后,我想到了以下两种方法:

1. 字符串的substring()方法

public static String capitalize(String str) {
    if(str== null || str.isEmpty()) {
        return str;
    }

    return str.substring(0, 1).toUpperCase() + str.substring(1);
}

例子:

System.out.println(capitalize("java")); // Java
System.out.println(capitalize("beTa")); // BeTa
System.out.println(capitalize(null)); // null

2. Apache Commons Lang

Apache Commons Lang库为此目的提供了StringUtils类:

System.out.println(StringUtils.capitalize("apache commons")); // Apache commons
System.out.println(StringUtils.capitalize("heLLO")); // HeLLO
System.out.println(StringUtils.uncapitalize(null)); // null

不要忘记在pom.xml文件中添加以下依赖项:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

要将字符串中每个单词的第一个字符大写,

首先,你需要获得该字符串的每个单词&对于这个分裂字符串,其中任何空间都存在,使用split方法如下所示,并将每个单词存储在一个数组中。 然后创建一个空字符串。之后,使用substring()方法获取对应单词的第一个字符和剩余字符,并将它们存储在两个不同的变量中。

然后使用toUpperCase()方法将第一个字符大写,并将如下所示的其余字符添加到空字符串中。

public class Test {  
     public static void main(String[] args)
     {
         String str= "my name is khan";        // string
         String words[]=str.split("\\s");      // split each words of above string
         String capitalizedWord = "";         // create an empty string

         for(String w:words)
         {  
              String first = w.substring(0,1);    // get first character of each word
              String f_after = w.substring(1);    // get remaining character of corresponding word
              capitalizedWord += first.toUpperCase() + f_after+ " ";  // capitalize first character and add the remaining to the empty string and continue
         }
         System.out.println(capitalizedWord);    // print the result
     }
}

你可以使用类WordUtils。

假设你的字符串是“当前地址”,然后使用

* * * *强烈textWordutils.capitaliz(字符串); 输出:当前地址

参见:http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html