java 计算字符串中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14469663/
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
Counting the spaces in a string
提问by S N Prasad Rao
I want to count the spaces in a string:
我想计算字符串中的空格:
public class SongApp {
public static void main(String[] args) {
String word = "a b c";
int i =0,spaceCount=0;
while(i<word.length()){
char temp = word.charAt(i);
System.out.println(temp);
if(" ".equals(temp)){
spaceCount++;
}
i++;
}
System.out.println("Spaces in string: "+spaceCount);
}
}
When I replace the if statement with if(temp.equals(" "))
, I get a "cannot invoke(String) on the primitive type char.
当我用 替换 if 语句时if(temp.equals(" "))
,我得到一个“无法在原始类型字符上调用(字符串)”。
I don't understand why this won't work.
我不明白为什么这行不通。
回答by paulolc
It won't work because you are calling a method of Class String (equals()) on a value which is of primitive type 'char'. You are trying to compare a 'char' with a 'String'.
它不起作用,因为您正在对原始类型“char”的值调用类 String (equals()) 的方法。您正在尝试将“字符”与“字符串”进行比较。
You must compare between 'char's and since it's a primitive value you need to use '==' boolean compare operator like:
您必须在 'char's 之间进行比较,因为它是一个原始值,所以您需要使用 '==' 布尔比较运算符,例如:
public class SongApp {
public static void main(String[] args) {
String word = "a b c";
int i = 0,
spaceCount = 0;
while( i < word.length() ){
if( word.charAt(i) == ' ' ) {
spaceCount++;
}
i++;
}
System.out.println("Spaces in string: "+spaceCount);
}
}
回答by Barinder
You can use the replace function for String to replace all the spaces(" ") with no spaces("") and get the difference between the lengths before and after calling the replace function. Go through this example:
您可以使用 String 的替换函数将所有空格(" ")替换为没有空格(""),并获取调用替换函数前后的长度差异。通过这个例子:
class Test{
public static void main(String args[]){
String s1 = "a b c";
int s1_length = s1.length();
System.out.println(s1_length); // 5
String s2 = s1.replace(" ","");
int s2_length = s2.length();
System.out.println(s2_length); // 3
System.out.println("No of spaces = " + (s1_length-s2_length)); // No of spaces = 2
}
}
回答by William Feirie
You can use commons-lang.jar to calculate this.
您可以使用 commons-lang.jar 来计算它。
`public class Main {
`公共类主{
public static void main(String[] args) {
String word = "a b c";
System.out.println("Spaces in string: " + StringUtils.countMatches(word," "));
}
}`
}`
The source of "StringUtils.countMatches" is below:
“StringUtils.countMatches”的来源如下:
public static int countMatches(String str, String sub) {
if (isEmpty(str) || isEmpty(sub)) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) {
count++;
idx += sub.length();
}
return count;
}
回答by S N Prasad Rao
public class CountSpace {
公共类计数空间{
public static void main(String[] args) {
String word = "a b c";
String data[];int k=0;
data=word.split("");
for(int i=0;i<data.length;i++){
if(data[i].equals(" ")){
k++;
}
}
System.out.println(k);
}
}
}