java 如何计算句子中的字母数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34471233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count number of letters in sentence
提问by Eli
I'm looking for simple way to find the amount of letters in a sentence. All I was finding during research were ways to find a specific letter, but not from all kinds.
我正在寻找一种简单的方法来查找句子中的字母数量。我在研究过程中发现的只是找到特定字母的方法,但不是所有类型的。
How I do that?
我怎么做?
What I currently have is:
我目前拥有的是:
sentence
= the sentence I get from themain
methodcount
= the number of letters I want give back to themain
method
sentence
= 我从main
方法中得到的句子count
= 我想回馈给main
方法的字母数
public static int countletters(String sentence) {
// ....
return(count);
}
回答by SMA
You could manually parse the string and count number of characters like:
您可以手动解析字符串并计算字符数,例如:
for (index = 1 to string.length()) {
if ((value.charAt(i) >= 'A' && value.charAt(i) <= 'Z') || (value.charAt(i) >= 'a' && value.charAt(i) <= 'z')) {
count++;
}
}
//return count
回答by Tom
A way to do this could stripping every unwanted character from the String and then check it's length. This could look like this:
一种方法可以从字符串中去除每个不需要的字符,然后检查它的长度。这可能是这样的:
public static void main(String[] args) throws Exception {
final String sentence = " Hello, this is the 1st example sentence!";
System.out.println(countletters(sentence));
}
public static int countletters(String sentence) {
final String onlyLetters = sentence.replaceAll("[^\p{L}]", "");
return onlyLetters.length();
}
The stripped String looks like:
剥离的字符串看起来像:
Hellothisisthestexamplesentence
你好这个最典型的句子
And the length of it is 31.
它的长度是31。
This code uses String#replaceAll
which accepts a Regular Expressionand it uses the category \p{L}
which matches every letter in a String. The construct [^...]
inverts that, so it replaces every character which is nota letter with an empty String.
此代码使用String#replaceAll
which 接受正则表达式,并使用\p{L}
与字符串中的每个字母匹配的类别。该构造将其[^...]
反转,因此它将不是字母的每个字符替换为空字符串。
Regular Expressions can be expensive (for the performance) and if you are bound to have the best performance, you can try to use other methods, like iterating the String, but this solution has the much cleaner code. So if clean code counts more for you here, then feel free to use this.
正则表达式可能很昂贵(为了性能),如果您一定要获得最佳性能,您可以尝试使用其他方法,例如迭代字符串,但此解决方案具有更清晰的代码。因此,如果干净的代码在这里对您来说更重要,那么请随意使用它。
Also mind that \\p{L}
detects unicode letters, so this will also correctly treat letters from different alphabets, like cyrillic. Other solutions currently only support latin letters.
还要注意\\p{L}
检测Unicode 字母,因此这也将正确处理来自不同字母表的字母,如西里尔字母。其他解决方案目前仅支持拉丁字母。
回答by Alexei
SMA's answer does the job, but it can be slightly improved:
SMA 的回答可以解决问题,但可以稍微改进一下:
public static int countLetters(String sentence) {
int count = 0;
for (int i = 0; i < sentence.length; i ++)
{
char c = Character.toUpperCase(value.charAt(i));
if (c >= 'A' && c <= 'Z')
count ++;
}
return count;
}
回答by funcoding
回答by svarog
use the .length()
method to get the length of the string, the length is the amount of characters it contains without the nullterm
使用.length()
方法获取字符串的长度,长度是它包含的字符数,不带nullterm
if you wish to avoid spaces do something like
如果您想避免空格,请执行以下操作
String input = "The quick brown fox";
int count = 0;
for (int i=0; i<input.length(); i++) {
if (input.charAt(i) != ' ') {
++count;
}
}
System.out.println(count);
if you wish to avoid other white spaces use a regex, you can refer to this questionfor more details
如果你想避免其他空格使用正则表达式,你可以参考这个问题了解更多细节