查找字符串变量中的位数

时间:2022-07-26 19:29:08

I have a string which sometimes gives character value and sometimes gives integer value. I want to get the count of number of digits in that string.

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

For example, if string contains "2485083572085748" then total number of digits is 16.

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

Please help me with this.

请帮我解决一下这个。

7 个解决方案

#1


12  

String s = "2485083572085748";
int count = 0;
for (int i = 0, len = s.length(); i < len; i++) {
    if (Character.isDigit(s.charAt(i))) {
        count++;
    }
}

#2


19  

A cleaner solution using Regular Expressions:

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

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

#3


2  

Loop each character and count it.

循环每个字符并计算它。

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

#4


2  

If your string gets to big and full of other stuff than digits you should try to do it with regular expressions. Code below would do that to you:

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

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

check out java regex lessons for more. cheers!

查看java正则表达式课程了解更多信息。干杯!

#5


2  

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

#6


0  

int count = 0;
for(char c: str.toCharArray()) {
    if(Character.isDigit(c)) {
        count++;
    }
}

Also see

另见

#7


-3  

Something like:

就像是:

using System.Text.RegularExpressions;


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

#1


12  

String s = "2485083572085748";
int count = 0;
for (int i = 0, len = s.length(); i < len; i++) {
    if (Character.isDigit(s.charAt(i))) {
        count++;
    }
}

#2


19  

A cleaner solution using Regular Expressions:

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

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

#3


2  

Loop each character and count it.

循环每个字符并计算它。

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

#4


2  

If your string gets to big and full of other stuff than digits you should try to do it with regular expressions. Code below would do that to you:

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

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

check out java regex lessons for more. cheers!

查看java正则表达式课程了解更多信息。干杯!

#5


2  

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

#6


0  

int count = 0;
for(char c: str.toCharArray()) {
    if(Character.isDigit(c)) {
        count++;
    }
}

Also see

另见

#7


-3  

Something like:

就像是:

using System.Text.RegularExpressions;


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