【问题标题】:Determine how many digits and letters are in a string [closed]确定字符串中有多少个数字和字母[关闭]
【发布时间】:2012-11-21 16:09:06
【问题描述】:
import java.util.Scanner;

public class digitthingy
{
    public static void main(String args[])
    {
        Scanner s = new Scanner(System.in);
    
        String first="";
        int firstnum=0;
    
        System.out.print("Enter a string: ");
        first = s.nextLine();
    
        firstnum = first.indexOf("1-100");
    
        System.out.println(firstnum);
    }
}

我想知道我输入的某个字符串中有多少个数字,但我不知道该怎么做。

【问题讨论】:

  • 数字是 2342、99 还是数字?

标签: java string parsing


【解决方案1】:

这看起来像是regular expressions 的工作!

String s = "sdf234sdf234";

System.out.println(s.replaceAll("\\D", "").length());

或者也许你在每个多位数字实例之后?

String s = "sdf234sdf234sdf23";

s = s.replaceAll("^\\D+|\\D+$", "").replaceAll("\\D+", ",");

List<String> numbers = Arrays.asList(s.split(","));

System.out.println(numbers);

【讨论】:

    【解决方案2】:
    String myString = "whatever123";
    int count = 0;
    for (int i = 0; i < myString.length(), i++) {
        if (Character.isDigit(myString.charAt(i)) {
            count++;  
        }
    }
    System.out.println(count);
    

    【讨论】:

    • 效果很好,很抱歉再次打扰您。但是你知道如何计算出有多少个字母吗?......比如说“这是 2 的 #1 测试”这样的字符串。
    • 使用 Character.isLetter()。我会保持两个计数,digitCount 和 letterCount。
    【解决方案3】:

    对于位数:

    Pattern digitPattern = Pattern.compile("\\d");
    Matcher digitMatcher = digitPattern.matcher("asdf 123 qwer");
    int digitCount = 0;
    while (digitMatcher.find()) 
       digitCount++;
    

    字母类似,只使用“[a-zA-Z]”正则表达式;

    【讨论】:

      【解决方案4】:

      用 java 8 计算字符串中的数字:

      public long countDigits(String s) {
          return s.chars().mapToObj(i -> (char) i).filter(Character::isDigit).count();
      }
      

      【讨论】:

        【解决方案5】:

        试试这个...

        public class digitthingy
        {
         public static void main(String args[])
         {
             Scanner sc = new Scanner(System.in);
        
            String s="";
            int firstnum=0;
        
            System.out.print("Enter a string: ");
            s = sc.nextLine();
            int digitCount =0, charCount=0;
            for(int i=0;i<s.length();i++){
        if(s.charAt(i)==' ') continue;
        else if((s.charAt(i) >='a' && s.charAt(i)<='z') || (s.charAt(i)>='A' && s.charAt(i)<='Z'))
            charCount++;
        else if(s.charAt(i) >= '0' && s.charAt(i)<='9')
            digitCount++;
           }
        System.out.println(digitCount+ " "+ charCount);
          }
          }
        

        【讨论】:

          【解决方案6】:

          您可以使用 Scanner 类:

             Scanner in = new Scanner(System.in);
          
             // Reads a single line from the console 
             // and stores into name variable
             first = s.nextLine();
          

          然后遍历字符串:

            int numbers = 0;
            int letters = 0;
          
            for(int i = 0; i < first.length(); i++)
            {
             if      (Character.isDigit (first.charAt(i)) numbers++;  // Count the digits
             else if (Character.isLetter(first.charAt(i)) letters++;   // Count the Letters
             else{} // is not a letter and not a digit
            } 
          

          【讨论】:

            【解决方案7】:

            试试这个代码我希望它会工作

            import java.util.Scanner;
            
            public class CountDigitAndCharacter {
            
                    public static void main(String args[])
                    {
                        Scanner s = new Scanner(System.in);
            
                        String first="";
                        int firstnum=0;
            
                        System.out.print("Enter a string: ");
                        first = s.nextLine();
                        int numberCount=0,charCount=0;
                        for(int i=0; i<first.length();i++){
                            char c=first.charAt(i);
                            if(c=='0' || c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9' )
                                ++numberCount;
                            else
                                ++charCount;    
                        }
            
                        System.out.println("numberCount  :-"+numberCount   +"  CharCount   :-"+charCount);
                    }
            }
            

            结果:- 输入一个字符串:l0kesh numberCount :-1 CharCount :-5

            【讨论】:

              【解决方案8】:
              public class ArrayDigits 
              
              {
                   public static void main(String[] args)
                   {
                      String str = new String("Hey123456789234Hey");
                      String num = new String("0123456789");
                      int dig_count = 0;
                  
                      for(int i=0;i<str.length();i++)
                      {
                         for(int j=0;j<num.length();j++)
                         {
                             if(str.charAt(i)==num.charAt(j))
                             {
                                dig_count++;
                            }
                        }
                      }
                      System.out.println("Total length of String:"+str.length());
                      System.out.println("Number of digits:"+dig_count);
                      System.out.println("Number of characters:"+(str.length() - dig_count));
                   }
              } 
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2022-11-16
                • 1970-01-01
                • 2019-11-03
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-12-01
                • 2014-08-08
                相关资源
                最近更新 更多