【问题标题】:Find count of digits in string variable查找字符串变量中的位数
【发布时间】:2011-04-06 09:41:12
【问题描述】:

我有一个字符串,它有时给出字符值,有时给出整数值。我想获取该字符串中的位数。

例如,如果字符串包含“2485083572085748”,则总位数为 16。

请帮帮我。

【问题讨论】:

  • 例如,如果字符串包含“2485083572085748”,则整数总数为 1。
  • @Andrey,他的意思似乎是说“数字”而不是“整数”。我已经编辑了问题。
  • @Vikram 我明白,这是我想指出的。
  • @Andrey,好的。知道了! :)

标签: java


【解决方案1】:

使用正则表达式的更简洁的解决方案:

// matches all non-digits, replaces it with "" and returns the length.
s.replaceAll("\\D", "").length()

【讨论】:

  • 我怀疑评估所有字符的正则表达式会比计算它们更快。但我喜欢简洁,所以有我的赞成票。
【解决方案2】:
String s = "2485083572085748";
int count = 0;
for (int i = 0, len = s.length(); i < len; i++) {
    if (Character.isDigit(s.charAt(i))) {
        count++;
    }
}

【讨论】:

  • 我故意避免toCharArray,因为根据我的测试它有点慢。此外,只计算一次长度应该会快一点。但是,Character.isDigit 可能不是最佳的;可能c &gt;= '0' &amp;&amp; c &lt;= '9' 更快。
【解决方案3】:

只是用计算字符串中的数字的流选项来刷新这个线程:

"2485083572085748".chars()
                  .filter(Character::isDigit)
                  .count();

【讨论】:

    【解决方案4】:

    如果您的字符串变得很大并且充满了数字以外的其他内容,您应该尝试使用正则表达式来完成。下面的代码会对你这样做:

    String str = "asdasd 01829898 dasds ds8898";
    
    Pattern p = Pattern.compile("\d"); // "\d" is for digits in regex
    Matcher m = p.matcher(str);
    int count = 0;
    while(m.find()){
       count++;
    }
    

    查看java regex lessons 了解更多信息。 干杯!

    【讨论】:

      【解决方案5】:

      循环每个字符并计数。

          String s = "2485083572085748";
          int counter = 0;
          for(char c : s.toCharArray()) {
              if( c >= '0' && c<= '9') {
                  ++counter;
              }
          }
          System.out.println(counter);
      

      【讨论】:

        【解决方案6】:
        public static int getCount(String number) {
            int flag = 0;
            for (int i = 0; i < number.length(); i++) {
                if (Character.isDigit(number.charAt(i))) {
                    flag++;
                }
            }
            return flag;
        }
        

        【讨论】:

          【解决方案7】:

          在 JavaScript 中:

          str = "2485083572085748"; //using the string in the question
          let nondigits = /\D/g;  //regex for all non-digits
          let digitCount = str.replaceAll(nondigits, "").length;
          //counts the digits after removing all non-digits
          console.log(digitCount); //see in console
          

          谢谢 --> https://stackoverflow.com/users/1396264/vedant 上面的 Java 版本。它也帮助了我。

          【讨论】:

            【解决方案8】:
            int count = 0;
            for(char c: str.toCharArray()) {
                if(Character.isDigit(c)) {
                    count++;
                }
            }
            

            另见

            【讨论】:

              【解决方案9】:

              类似:

              using System.Text.RegularExpressions;
              
              
                          Regex r = new Regex( "[0-9]" );
                          Console.WriteLine( "Matches " + r.Matches("if string contains 2485083572085748 then" ).Count );
              

              【讨论】:

                猜你喜欢
                • 2016-06-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-06-12
                • 2019-07-31
                • 2020-11-07
                • 2011-04-13
                相关资源
                最近更新 更多