确定字符串中的数字和字母数

时间:2012-12-04 04:19:49

标签: java string parsing

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

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

8 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分:1)

对于位数:

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

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

答案 3 :(得分:1)

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

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

答案 4 :(得分: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
  } 

答案 5 :(得分: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);
  }
  }

答案 6 :(得分: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));
}

}

答案 7 :(得分: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