Java 忽略空格,只计算字母

Java 忽略空格,只计算字母,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+

你能不能给我们的程序一个主意,忽略空格,只计算字母,甚至是小的其他明智的大写字母

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

    }
}
公共类evennumbersloop{
公共静态void main(字符串[]args){
String str=“你好世界UUU iii pppp jjj”;
整数计数=0;

对于(int i=0;i要仅计算字母,可以使用正则表达式并编写如下代码:

public static void main(String[] args) {
   Pattern pattern = Pattern.compile("\\p{Alpha}");
   Matcher matcher = pattern.matcher("Hello World uuuu iii pppp jjj");
   int count = 0;
   while (matcher.find())
       count++;
   System.out.println(count);
}
你是说

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

我不知道你在问什么。你能回答你的问题并澄清一下吗?顺便说一句,
Character
类有很多有用的方法,比如
Character.isleter
(如果这就是你所说的“字母表”)。举例说明你得到了什么和你想要什么?你的想法是什么意思?你有什么问题吗?你能澄清一下吗?请看。然后试着用
“Hello World\t这是我!”
你至少应该用
\s
匹配每个“空白”字符。