locked
Possible to tell if a string contains a number? RRS feed

  • Question

  • I am trying to figure out if it is possible to tell if a string contains a number. Not if a string is numeric though (although it could be..)

    For example I have several strings that may look like this:

    20ship
    70driver
    John Doe

    I want to identify that the first 2 strings have numbers in them and not display them in a combo box. I am assuming I need to do some type of regex but i'm not sure how to do that in C# (or even that familiar with regex in general).

     

     


    Wednesday, August 17, 2011 12:17 AM

Answers

  • You can do it in one line using Linq,

    string input = "hello123world";
    bool isDigitPresent = input.Any(c => char.IsDigit(c));

    Hope you like this :)


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by RohitArora Wednesday, August 17, 2011 10:32 AM
    • Marked as answer by C-M Wednesday, August 17, 2011 4:08 PM
    Wednesday, August 17, 2011 3:07 AM
  • bool isInputInvalid = input.Any(c => char.IsDigit(c)) || input[0] == '$';


    I would test the $ before scanning the string, and check its length. And add a 'not' operator because it's not invalid if there is a digit.

    bool isInputInvalid = string.IsNullOrEmpty(input) || input[0] == '$' || !input.Any(c => char.IsDigit(c));
    
    

     


    • Marked as answer by C-M Wednesday, August 17, 2011 4:08 PM
    Wednesday, August 17, 2011 3:24 PM

All replies

  • The regular expression would be \r+.  Here is an example code with your test strings:
     
      static void Main(string[] args)
      {
       string[] tests = new string[] { "20ship", "70driver", "John Doe" };
     
       Regex r = new Regex(@"\d+");
       foreach (string test in tests)
        if (r.IsMatch(test))
         Console.WriteLine("Match: {0}", test);
        else
         Console.WriteLine("No Match: {0}", test);
     
       Console.ReadKey();
      }

     

    --
    Mike
    Wednesday, August 17, 2011 1:28 AM
  •  You can also try

    Regex.Match("your string", @".*([\d]+).*")



    My Blogs
    Wednesday, August 17, 2011 2:52 AM
  • You can do it in one line using Linq,

    string input = "hello123world";
    bool isDigitPresent = input.Any(c => char.IsDigit(c));

    Hope you like this :)


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by RohitArora Wednesday, August 17, 2011 10:32 AM
    • Marked as answer by C-M Wednesday, August 17, 2011 4:08 PM
    Wednesday, August 17, 2011 3:07 AM
  • Very nice thanks for all the options. I am going to try the linq one when I get into work tomorrow just because I have not used linq yet :) I should be able to figure out how to use one of these as I pull them out of our AD. Will mark answer once I get it working.
    Wednesday, August 17, 2011 3:20 AM
  • You really only need to detect one digit:

    Regex r = new Regex(@"\d");
    

     

    Wednesday, August 17, 2011 8:22 AM
  •  You can also try

    Regex.Match("your string", @".*([\d]+).*")



    My Blogs

    Adding .* at the start and the end of a regex is useless.
    Wednesday, August 17, 2011 8:24 AM
  • Ok now I just remembered we are also interested in not showing strings that start with a $ as well. since I know the $ is a special char in regex can one of you tell me how to match that char? Thanks for all of the suggestions so far!
    Wednesday, August 17, 2011 2:03 PM
  • Check this out,

    string input = "hello123world";
    
    bool isInputInvalid = input.Any(c => char.IsDigit(c)) || input[0] == '$';


    Please mark this post as answer if it solved your problem. Happy Programming!


    Wednesday, August 17, 2011 2:28 PM
  • To use $ or any other special character in a regex, you escape it with a backslash. Excluding $ at the start of the string could be done with

    ^(?!\$).*\d

    But using Adavesh's code is simpler.

    Wednesday, August 17, 2011 3:22 PM
  • bool isInputInvalid = input.Any(c => char.IsDigit(c)) || input[0] == '$';


    I would test the $ before scanning the string, and check its length. And add a 'not' operator because it's not invalid if there is a digit.

    bool isInputInvalid = string.IsNullOrEmpty(input) || input[0] == '$' || !input.Any(c => char.IsDigit(c));
    
    

     


    • Marked as answer by C-M Wednesday, August 17, 2011 4:08 PM
    Wednesday, August 17, 2011 3:24 PM
  • Thanks you guys rock. I was trying to go more complicated than I needed to with the $ totally spaced the easy way. Everyone was very helpful and I managed to get the linq way working.
    Wednesday, August 17, 2011 4:07 PM