简体   繁体   English

将字符串中的第一个字母大写。 我究竟做错了什么?

[英]Capitalizing the first letter in a string. What am I doing wrong?

What am I doing wrong? 我究竟做错了什么? When I run my program it only prints some of my string, and it does not capitalize the first letter.. 当我运行程序时,它只会打印一些字符串,而不会大写第一个字母。

public class StringTraining extends ConsoleProgram {
    public void run() {
        String str = "halOOlOO";
        capitalize(str);
    }


    private String capitalize(String str){
        String s = "";
        char ch;
        for(int i = 0;i<str.length();i++) {
            ch = str.charAt(i);
            if(i==0 && Character.isLowerCase(ch)) {
                Character.toUpperCase(ch);
                s += ch;
                i++;
            } else {
                Character.toLowerCase(ch);
                s += ch;
                i++;
            }
        }
        println(s);
        return s;
    }

}

You need to assign the variable ch to the upper or lower case value: 您需要将变量ch分配给大写或小写值:

for(int i = 0;i<str.length();i++) {
        ch = str.charAt(i);
        if(i==0 && Character.isLowerCase(ch)) {
            ch = Character.toUpperCase(ch);
            s += ch;
        } else {
            ch = Character.toLowerCase(ch);
            s += ch;
        }
    }
  1. You should not increment i again in the loop since it will be done automatically in the signature of the loop. 您不应在循环中再次递增i ,因为它将在循环的签名中自动完成。

  2. You have to assign Character.toUpperCase(ch) to the String or append it. 您必须将Character.toUpperCase(ch)分配给String或附加它。

  3. I'd suggest you use a StringBuilder when looping to build a String object 我建议您在循环构建String对象时使用StringBuilder


Correction 更正

private static String capitalize(String str){
    StringBuilder s = new StringBuilder();
    char ch;
    for(int i = 0;i<str.length();i++) {
        ch = str.charAt(i);
        if(i==0 && Character.isLowerCase(ch)) {
            s.append(Character.toUpperCase(ch));
        } else {
            s.append(Character.toLowerCase(ch));
        }
    }
    return s.toString();
}

Output 输出量

Halooloo

Remove some unnecessary codes from your capitalize(String) method such as i++ and use 从您的capitalize(String)方法(例如i++ capitalize(String)删除一些不必要的代码,然后使用
s += String.valueOf(Character.toUpperCase(ch)); code instead of 代码代替

Character.toUpperCase(ch);
s += ch;

Complete capitalize(String) method 完整的大写(String)方法

private static String capitalize(String str) {
    String s = "";
    char ch;
    for (int i = 0; i < str.length(); i++) {
        ch = str.charAt(i);
        if (i == 0 && Character.isLowerCase(ch)) {
            s += String.valueOf(Character.toUpperCase(ch));
        } else {
            s += String.valueOf(Character.toLowerCase(ch));
        }
    }
    println(s);
    return s;
}

toLowerCase() return a string, you need to assign it to ch . toLowerCase()返回一个字符串,您需要将其分配给ch You also need to increment your i only one time (in the for , and not in the if ) 您还需要仅将i递增一次(在for ,而不在if

Change your capitalize(String str) method like this - 像这样更改您的capitalize(String str)方法-

 private static String capitalize(String str) {
    char[] chars = str.toCharArray();
    String caps = chars[0]+"";
    caps = caps.toUpperCase();
    String output = caps;
    for(int i=1;i<chars.length;i++) {
        output = output + chars[i];
    }
    return output;
}

Output : 输出:

HalOOlOO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM