我使用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()


当前回答

你可以试试这个

/**
 * capitilizeFirst(null)  -> ""
 * capitilizeFirst("")    -> ""
 * capitilizeFirst("   ") -> ""
 * capitilizeFirst(" df") -> "Df"
 * capitilizeFirst("AS")  -> "As"
 *
 * @param str input string
 * @return String with the first letter capitalized
 */
public String capitilizeFirst(String str)
{
    // assumptions that input parameter is not null is legal, as we use this function in map chain
    Function<String, String> capFirst = (String s) -> {
        String result = ""; // <-- accumulator

        try { result += s.substring(0, 1).toUpperCase(); }
        catch (Throwable e) {}
        try { result += s.substring(1).toLowerCase(); }
        catch (Throwable e) {}

        return result;
    };

    return Optional.ofNullable(str)
            .map(String::trim)
            .map(capFirst)
            .orElse("");
}

其他回答

字符串首字母大写的更短/更快的版本代码是:

String name  = "stackoverflow"; 
name = name.substring(0,1).toUpperCase() + name.substring(1).toLowerCase();

name的值为Stackoverflow

那些谁搜索首字母大写的名字在这里..

public static String capitaliseName(String name) {
    String collect[] = name.split(" ");
    String returnName = "";
    for (int i = 0; i < collect.length; i++) {
        collect[i] = collect[i].trim().toLowerCase();
        if (collect[i].isEmpty() == false) {
            returnName = returnName + collect[i].substring(0, 1).toUpperCase() + collect[i].substring(1) + " ";
        }
    }
    return returnName.trim();
}

usase: capitaliseName(“saurav khan”); 输出:Saurav Khan

您可以使用以下代码:

public static String capitalizeString(String string) {

    if (string == null || string.trim().isEmpty()) {
        return string;
    }
    char c[] = string.trim().toLowerCase().toCharArray();
    c[0] = Character.toUpperCase(c[0]);

    return new String(c);

}

使用JUnit的示例测试:

@Test
public void capitalizeStringUpperCaseTest() {

    String string = "HELLO WORLD  ";

    string = capitalizeString(string);

    assertThat(string, is("Hello world"));
}

@Test
public void capitalizeStringLowerCaseTest() {

    String string = "hello world  ";

    string = capitalizeString(string);

    assertThat(string, is("Hello world"));
}

你要做的可能是:

s1 = name.substring(0, 1).toUpperCase() + name.substring(1);

(将第一个字符转换为大写,并添加原始字符串的其余部分)

此外,您创建了一个输入流读取器,但从不读取任何行。因此name将始终为空。

这应该可以工作:

BufferedReader br = new InputstreamReader(System.in);
String name = br.readLine();
String s1 = name.substring(0, 1).toUpperCase() + name.substring(1);

那么wordutils . capitalizately()呢?

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);
    }
}