1

我需要知道一个字符串中有多少相同类型的字符。

我试过这个

String x ="(3+3)*(4-2)";
int a = x.indexOf( "(" );

但这只会给我第一个索引

4

8 回答 8

1

您可以使用循环并使用其他方法indexOf(int, int)

String x ="(3+3)*(4-2)";
int a = x.indexOf( "(" );

while (a >= 0) {
    System.out.println("Char '(' found at: "+a);
    a = x.indexOf('(', a+1);
}
于 2013-09-06T07:03:10.820 回答
0

令人难以置信的是,这样一个简单问题的答案竟然如此复杂。

x.indexOf( "(" ); 但这只会给我第一个索引

用于x.indexOf( "(", fromIndex );查找更多事件。观点。

顺便说一句,如果您正在寻找单个字符,您可以使用x.indexOf( '(');x.indexOf( '(', fromIndex );提高效率。

因此,无需重新发明轮子的最有效方法是:

int count=0;
for(int pos=s.indexOf('('); pos!=-1; pos=s.indexOf('(', pos+1)) count++;
于 2013-09-06T07:46:11.987 回答
0

有几种方法我可以想到这样做,但最简单的方法之一是简单地循环String....

String x ="(3+3)*(4-2)";
int count = 0;
for (char c : x.toCharArray()) {
    if (c == '(') {
        count++;
    }
}
System.out.println(count);

仅仅因为它可以完成......你可以使用一点正则表达式......(我知道,矫枉过正)

Pattern p = Pattern.compile("\\(");
Matcher matcher = p.matcher(x);
while (matcher.find()) {
    count++;
}
System.out.println(count);
于 2013-09-06T06:58:32.650 回答
0

使用StringUtils.countMatches

StringUtils.countMatches(value,"(");

或者

public static int countMatches(String value, String valueToCount) {
          if (value.isEmpty() || valueToCount.isEmpty()) {
                return 0;
            }
            int count = 0;
            int index = 0;
            while ((index = value.indexOf(valueToCount, index)) !=  -1) {
                count++;
                index += valueToCount.length();
            }
            return count;
        }
于 2013-09-06T06:57:23.450 回答
0

似乎最好把它放在一个单独的函数中:

// accepts a string and a char to find the number of occurrences of
public static int get_count(String s, char c) {    

    int count = 0;                        // count initially 0

    for (int i = 0; i < s.length(); i++)  // loop through the whole string
        if (s.charAt(i) == c)
            count ++;                     // increment every time an occurrence happens

    return count;                         // return the count in the end

}

你可以这样称呼它:

System.out.println(get_count("(3+3)*(4-2)", '('));

// Output: 2
于 2013-09-06T07:07:25.670 回答
0

下面的代码可以满足您的需求。如果性能很关键,您可以使用它进行优化。如果您想要更优雅的解决方案,您可以查看 java 的正则表达式库。

int occurences = 0;
String x ="(3+3)*(4-2)";
char tolookfor = '(';
for(int i = 0; i < x.length() ; i++)
{
    if(x.charAt(i) == tolookfor)
        occurences++;
}
于 2013-09-06T07:07:27.617 回答
0

你可以试试这个

    String x ="(3+3)*(4-2)";
    char[] arr=x.toCharArray();
    Map<String,Integer> map=new HashMap<>();
    for(int i=0;i<arr.length;i++){
          Integer upTo=map.get(String.valueOf(arr[i]));
           if (upTo==null) {
               upTo=0;
           }
          map.put(String.valueOf(arr[i]),upTo+1) ;
    }
    for (Map.Entry<String,Integer> entry:map.entrySet()){
        System.out.println("Number of "+entry.getKey()+" in this string is: "+entry.getValue());
    }

输出

Number of 3 in this string is: 2
Number of 2 in this string is: 1
Number of 4 in this string is: 1
Number of * in this string is: 1
Number of + in this string is: 1
Number of ( in this string is: 2
Number of ) in this string is: 2
Number of - in this string is: 1
于 2013-09-06T07:14:59.880 回答
0

这将帮助你!

public static int counter(String x, char y) {

    char[] array=x.toCharArray();

    int count=0;
    for(int i=0;i<x.length();i++)
    {
        if(y==array[i]) count++;

    }

    return (count>0)? count:0;  
}
于 2019-04-12T15:06:34.023 回答