get the count of numbers in a string in csharp

Here's an example of how to get the count of numbers in a string in C#:

main.cs
string inputString = "1a3b5c7";
int count = 0;

foreach (char c in inputString)
{
    if (char.IsDigit(c))
    {
        count++;
    }
}

Console.WriteLine("Number of digits in the string: " + count);
202 chars
13 lines

In this code snippet, we first declare a string variable inputString and initialize it with the string that we want to count the numbers of. We then declare an integer variable count and assign it an initial value of 0.

We then loop through each character in the string using a foreach loop. For each character, we use the char.IsDigit method to check if the character is a digit or not. If it is a digit, we increment the count variable.

Finally, we print out the result by outputting the value of count.

related categories

gistlibby LogSnag