none
How can I count how many nnumbers are in a string? RRS feed

  • Question

  • Hi, could someone show me how to count how many numbers are in a string. For example, the string "there are 32 kids in this class, and 31 kids in that one", has two numbers in it. I want to be able to count the number of numbers.

    Thanks

    Saturday, January 4, 2020 10:20 PM

Answers

  • Here is a console app, source from here.

    using System;
    using System.Text.RegularExpressions;
    
    namespace Console_cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string input = "there are 32 kids in this class, and 31 kids in that one";
                // Split on one or more non-digit characters.
                string[] numbers = Regex.Split(input, @"\D+");
                foreach (string value in numbers)
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        int i = int.Parse(value);
                        Console.WriteLine("Number: {0}", i);
                    }
                }
    
                Console.ReadLine();
            }
        }
    }

    Or

    using System;
    using System.Text.RegularExpressions;
    
    namespace NorthEntityConsole_cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string input = "there are 32 kids in this class, and 31 kids in that one";
                // Split on one or more non-digit characters.
                string[] numbers = Regex.Split(input, @"\D+");
                var mc = Regex.Matches(input, "\\d+(\\.\\d+)?");
                foreach (Match m in mc)
                {
                    double d = double.Parse(m.Groups[0].Value);
                    Console.WriteLine(d);
                }
    
                Console.ReadLine();
            }
        }
    }

    Or

    using System;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    namespace NorthEntityConsole_cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string input = "there are 32 kids in this class, and 31 kids in that one";
                // Split on one or more non-digit characters.
                string[] numbers = Regex.Split(input, @"\D+");
                var resultArray = Regex.Split(input, @"[^0-9\.]+")
                    .Where(c => c != "." && c.Trim() != "");
    
                foreach (var value in resultArray)
                {
                    Console.WriteLine(value);
                }
    
                Console.ReadLine();
            }
        }
    }
    Use .Count() to get the count in each array


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange




    Saturday, January 4, 2020 10:30 PM
    Moderator

All replies

  • Here is a console app, source from here.

    using System;
    using System.Text.RegularExpressions;
    
    namespace Console_cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string input = "there are 32 kids in this class, and 31 kids in that one";
                // Split on one or more non-digit characters.
                string[] numbers = Regex.Split(input, @"\D+");
                foreach (string value in numbers)
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        int i = int.Parse(value);
                        Console.WriteLine("Number: {0}", i);
                    }
                }
    
                Console.ReadLine();
            }
        }
    }

    Or

    using System;
    using System.Text.RegularExpressions;
    
    namespace NorthEntityConsole_cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string input = "there are 32 kids in this class, and 31 kids in that one";
                // Split on one or more non-digit characters.
                string[] numbers = Regex.Split(input, @"\D+");
                var mc = Regex.Matches(input, "\\d+(\\.\\d+)?");
                foreach (Match m in mc)
                {
                    double d = double.Parse(m.Groups[0].Value);
                    Console.WriteLine(d);
                }
    
                Console.ReadLine();
            }
        }
    }

    Or

    using System;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    namespace NorthEntityConsole_cs
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string input = "there are 32 kids in this class, and 31 kids in that one";
                // Split on one or more non-digit characters.
                string[] numbers = Regex.Split(input, @"\D+");
                var resultArray = Regex.Split(input, @"[^0-9\.]+")
                    .Where(c => c != "." && c.Trim() != "");
    
                foreach (var value in resultArray)
                {
                    Console.WriteLine(value);
                }
    
                Console.ReadLine();
            }
        }
    }
    Use .Count() to get the count in each array


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange




    Saturday, January 4, 2020 10:30 PM
    Moderator
  • could someone show me how to count how many numbers are in a string. For example, the string "there are 32 kids in this class, and 31 kids in that one", has two numbers in it. I want to be able to count the number of numbers.

    This has the look and feel of a course assignment. Trivial tasks involving
    students, or grades, or course subjects, etc. (academic nouns) usually are
    found in the context of a learning environment. Rarely if ever are they
    likely to be a "real world" task. Especially when the asker is apparently
    just learning the programming language.

    Now we try very hard - at least most of us do - to avoid giving complete and
    workable code solutions to students' assignments. That's cheating and the
    student is robbed of an opportunity to learn by experience, trial and error, 
    etc. I *could* give you a working code solution that needs less than six
    lines of code. But I won't, and hopefully neither will anyone else.

    What I will do is make some suggestions so you can produce the code yourself.
    Note that the best solution is one that uses what you have been taught in class
    so far. But we don't know what that is. You do - or should - and your solution
    should reflect that.

    Here's one approach:

    (1) Use string.Split to break your string into an array of substrings.
    (2) Use foreach to iterate over that array.
    (3) Test each string in the array using TryParse (e.g.  int.TryParse)
        to see if it can be converted to an integer. (You can do the same
        for doubles, etc. if needed).
    (4) If the TryParse succeeds then add one to the count of numbers in the string.

    - Wayne
        

    Sunday, January 5, 2020 7:23 AM
  • Consider this approach too:

     

    • Use Regex.Matches to find all the sequences. If integer numbers only are expected, the pattern is @"\d+".
    • Use Count on returned value to get the number of found numbers.

     


    • Edited by Viorel_MVP Monday, January 6, 2020 6:17 AM
    Monday, January 6, 2020 6:11 AM