简体   繁体   English

java中如何计算以大写字母开头的单词?

[英]How do I count the words that start with an uppercase letter in java?

I want to make a program that prints the number of words that START with an uppercase letter.我想打一个打印的话那START数以大写字母的程序。 So I made two strings str1 = "The deed is done" and str2 = "My name is Bond, JAMES Bond" .所以我做了两个字符串str1 = "The deed is done"str2 = "My name is Bond, JAMES Bond" For the first string, it printed 1 which is what I wanted.对于第一个字符串,它打印了我想要的 1。 But for the second one it prints 8 instead of 4 because JAMES is capitalized.但是对于第二个,它打印 8 而不是 4,因为JAMES是大写的。


    public static void main(String[] args){
        String str1 = "The deed is done";
        String str2 = "My name is Bond, JAMES Bond";

        System.out.println(uppercase(str2));
    }

    public static int uppercase(String str){
        int cnt = 0;

        for(int i = 0; i < str.length(); i++){
            if(Character.isUpperCase(str.charAt(i)))
                cnt++;
        }

        return cnt;
    }

That's what I have so far.这就是我到目前为止所拥有的。 How would I make it so that the other letters in that word aren't counted?我如何才能不计算该单词中的其他字母?

You should check the first character of each word in the input string, instead of all characters of the input string.您应该检查输入字符串中每个单词第一个字符,而不是输入字符串的所有字符

public static int uppercase(String str){
    int cnt = 0;

    String[] words = str.split(" ");

    for(int i = 0; i < words.length; i++){
        if(Character.isUpperCase(words[i].charAt(0)))
            cnt++;
    }

    return cnt;
}

A more 'declarative approach' could use a Stream更“声明性方法”可以使用Stream

public static long uppercase2(String str){
    return Arrays.stream(str.split(" "))
            .map(word -> word.charAt(0))
            .filter(Character::isUpperCase)
            .count();
}
String str1 = "The deed is done"; String str2 = "My name is Bond, JAMES Bond"; System.out.println(upperCaseCount(str1)); System.out.println(upperCaseCount(str2)); public static int upperCaseCount(String s) { int count = 0; // append a space to cater for empty string and // use regex to split on one or more spaces. for (String word : (s + " ").split("\\s+")) { if (Character.isUpperCase(word.charAt(0))) { count++; } } return count; }

A nice way to do this is with a regex: \\b[AZ] tests for capital letters occurring after a word boundary, so we can just find all matches and count them.一个很好的方法是使用正则表达式: \\b[AZ]测试出现在单词边界之后的大写字母,因此我们可以找到所有匹配项并计算它们。

> import java.util.regex.*;
> Pattern p = Pattern.compile("\\b[A-Z]");
> Matcher m = p.matcher("Hi, this is Stack Overflow.");
> int c = 0;
> while(m.find()) { c++; }
> c
3

I want to make a program that prints the number of words that START with an uppercase letter.我想打一个打印的话那START数以大写字母的程序。 So I made two strings str1 = "The deed is done" and str2 = "My name is Bond, JAMES Bond" .因此,我创建了两个字符串: str1 = "The deed is done"str2 = "My name is Bond, JAMES Bond" For the first string, it printed 1 which is what I wanted.对于第一个字符串,它打印了1,这正是我想要的。 But for the second one it prints 8 instead of 4 because JAMES is capitalized.但是对于第二个,它会打印8而不是4,因为JAMES是大写的。


    public static void main(String[] args){
        String str1 = "The deed is done";
        String str2 = "My name is Bond, JAMES Bond";

        System.out.println(uppercase(str2));
    }

    public static int uppercase(String str){
        int cnt = 0;

        for(int i = 0; i < str.length(); i++){
            if(Character.isUpperCase(str.charAt(i)))
                cnt++;
        }

        return cnt;
    }

That's what I have so far.到目前为止,这就是我所拥有的。 How would I make it so that the other letters in that word aren't counted?我将如何做到这一点,以便不计入该单词中的其他字母?

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

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