java 确定一个字符串中有多少个数字和字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13696027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:40:29  来源:igfitidea点击:

Determine how many digits and letters are in a string

javastringparsing

提问by user1840435

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

I'd like to know how many numbers there are in a certain string I entered, but I have no idea how to do it. I'm new to programming.

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

回答by Peter Wilkinson

This looks like a job for regular expressions!

这看起来像是一份工作regular expressions

String s = "sdf234sdf234";

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

Or perhaps your after each multiple digit number instance?

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

String s = "sdf234sdf234sdf23";

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

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

System.out.println(numbers);

回答by ccleve

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

回答by Delorean

For number of digits:

对于位数:

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

Similar for letters, only use "[a-zA-Z]" regex instead;

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

回答by 7er

Count digits in string with java 8:

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

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

回答by Lokesh Kumar Gaurav

Try this code I hope it will work

试试这个代码我希望它会起作用

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

Result:- Enter a string: l0kesh numberCount :-1 CharCount :-5

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

回答by dreamcrash

you can use the Scanner class:

您可以使用 Scanner 类:

   Scanner in = new Scanner(System.in);

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

And then iterate over the String:

然后遍历字符串:

  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
  } 

回答by Srinivas B

try this ...

试试这个 ...

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

回答by SanjayDVG

public class ArrayDigits

公共类 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));
}

}

}