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

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

4

8 回答 8

5

这看起来像是一份工作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);
于 2012-12-04T04:24:28.507 回答
1

对于位数:

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

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

于 2015-03-19T15:54:49.017 回答
1
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);
于 2012-12-04T04:23:13.617 回答
1

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

public long countDigits(String s) {
    return s.chars().mapToObj(i -> (char) i).filter(Character::isDigit).count();
}
于 2016-08-19T05:49:13.053 回答
0
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));
     }
} 
于 2015-02-26T10:18:45.017 回答
0

尝试这个 ...

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);
  }
  }
于 2012-12-04T04:29:50.997 回答
0

您可以使用 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
  } 
于 2012-12-04T04:24:49.547 回答
0

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

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

于 2016-08-19T06:18:10.113 回答