【问题标题】:Same char in one string一个字符串中的相同字符
【发布时间】:2013-09-10 04:41:38
【问题描述】:

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

我试过了

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

但这只会给我第一个索引

【问题讨论】:

    标签: java string types char


    【解决方案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);
    }
    

    【讨论】:

    • 哦,我忽略了其他所有人之间的答案。很高兴看到有其他人知道有该方法并在没有开销的情况下提供答案。
    【解决方案2】:

    好像把它放在一个单独的函数里会更好:

    // 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
    

    【讨论】:

      【解决方案3】:

      有几种方法我可以想到这样做,但最简单的方法之一是简单地循环 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);
      

      【讨论】:

        【解决方案4】:

        下面的代码可以满足您的需求。如果性能很关键,您可以使用它进行优化。如果你想要更优雅的解决方案,你可以看看 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++;
        }
        

        【讨论】:

          【解决方案5】:

          你可以试试这个

              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
          

          【讨论】:

            【解决方案6】:

            难以置信,这么简单的问题的答案竟然如此复杂。

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

            【讨论】:

              【解决方案7】:

              使用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;
                      }
              

              【讨论】:

              • 我不认为仅仅为了这样一个微不足道的任务而添加第三方库是一个好建议。但是在复制方法的代码时,您不应将第二个参数重命名为“regex”,因为它不会将其视为正则表达式。它正在调用 String.indexOf,它只进行完全匹配。
              【解决方案8】:

              这对你有帮助!

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-02-09
                • 1970-01-01
                • 1970-01-01
                • 2016-08-20
                相关资源
                最近更新 更多