有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java忽略空格,只计算字母

你能给我们的程序一个想法,忽略空格,只计算字母表,甚至是小的其他明智的上限吗

public class evennumbersloop {

    public static void main(String[] args) {

        String str = "Hello World uuuu iii pppp jjj";

        int count = 0;

        for (int i = 0; i <= str.length() - 1; i++) {

            if (str.charAt(i) != ' ') {
                if (count % 2 == 0) {
                    String str1 = "";
                    str1 = str1 + str.charAt(count);
                    System.out.print(str1.toUpperCase());
                } else {
                    System.out.print(str.charAt(count));
                }

                count++;
            }
        }

    }
}

共 (1) 个答案

  1. # 1 楼答案

    你是说

    String str = "Hello World uuuu iii pppp jjj";
    

    忽略空白并计算出现的字母数>;实际上是没有空格的字符串长度

    String str = "Hello World uuuu iii pppp jjj";
    System.out.println("Length of string with spaces: "+str.length());
    str = str.replaceAll("\\s", "");
    System.out.println("Length of String without white spaces: "+str.length());
    

    输出:

    Length of string with spaces: 29
    Length of String without white spaces: 24