8 Replies - 1199 Views - Last Post: 08 March 2013 - 11:39 AM Rate Topic: -----

#1 jmProg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Determining if a string contains numbers

Posted 06 March 2013 - 06:24 AM

Hello, I have a problem.

(I am using Visual C#)

I need to determine if a string has a number. If it contains a number. An error message must show saying that it is invalid. That letters are only accepted..

I have this code:

txtFirst.Text = "981298ABCD";
if (int.TryParse(txtFirst.Text, out input))
{
   MessageBox.Show("Invalid");
}
else
{
    MessageBox.Show("Letters");
}



Now, with that, it works ok.. but when the Letters are first entered..

txtFirst.Text = "ABCD1223";
if (int.TryParse(txtFirst.Text, out input))
{
   MessageBox.Show("Invalid");
}
else
{
    MessageBox.Show("Letters");
}



It proceed with the else statement.. How can I say it is invalid too?
I need advice. Thank you so much..

Is This A Good Question/Topic? 0
  • +

Replies To: Determining if a string contains numbers

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Determining if a string contains numbers

Posted 06 March 2013 - 06:28 AM

You can use regular expressions to do this -

bool isAllLetters = Regex.IsMatch(input, @"^[a-zA-Z]+$");


or if you have special characters that are accepted

bool isNoNumbers = Regex.IsMatch(input, @"^[^0-9]+$");

Was This Post Helpful? 0
  • +
  • -

#3 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Determining if a string contains numbers

Posted 06 March 2013 - 07:08 AM

View PostjmProg, on 06 March 2013 - 06:24 AM, said:

Hello, I have a problem.

(I am using Visual C#)

I need to determine if a string has a number. If it contains a number. An error message must show saying that it is invalid. That letters are only accepted..

I have this code:

txtFirst.Text = "981298ABCD";
if (int.TryParse(txtFirst.Text, out input))
{
   MessageBox.Show("Invalid");
}
else
{
    MessageBox.Show("Letters");
}



Now, with that, it works ok.. but when the Letters are first entered..

txtFirst.Text = "ABCD1223";
if (int.TryParse(txtFirst.Text, out input))
{
   MessageBox.Show("Invalid");
}
else
{
    MessageBox.Show("Letters");
}



It proceed with the else statement.. How can I say it is invalid too?
I need advice. Thank you so much..


I curently reinstall Visual Studio and i can't to test this, but i think that this will work fine.
Remember String is array of the characters.

So you can also use foreach statement
string s = "ABC123";
int num;
foreach(char c in s){
   if(int.tryParse(c,out num)){
         //Success
   }
   else{
        //Error
   }
}


This post has been edited by DocNet: 06 March 2013 - 07:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Determining if a string contains numbers

Posted 06 March 2013 - 07:24 AM

View PostRyano121, on 06 March 2013 - 05:28 AM, said:

bool isNoNumbers = Regex.IsMatch(input, @"^[^0-9]+$");

Good start, but overly complicated for the regex engine.
Boolean containsNumber = Regex.IsMatch(input, @"\d");

This post has been edited by Momerath: 06 March 2013 - 07:25 AM

Was This Post Helpful? 1
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Determining if a string contains numbers

Posted 06 March 2013 - 07:31 AM

Wow haha not sure how I didn't think of that one!
Was This Post Helpful? 0
  • +
  • -

#6 jmProg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Re: Determining if a string contains numbers

Posted 08 March 2013 - 05:36 AM

Ok, now I understand. Thanks a lot folks! :)
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Determining if a string contains numbers

Posted 08 March 2013 - 06:40 AM

Ouch! So you need to spin up an entire Regex to just check to see if a string contains numbers. I hope that you realize that a Regex is a state machine. If you've ever written a state machine by hand, you'll know how code and space intensive this can be. With a Regex, the class writes, the code, compiles it, and then runs your strings through that dynamically created code.

Why not a simple loop instead?

If you know you are only looking for base 10 digits:
bool HasNumbers(string s)
{
    foreach(char c in s)
        if (Char.IsDigit(c))
            return true;
    return false;
}


And if you are looking other potential numbers like fraction or roman numerals, use IsNumber() instead of IsDigit().

This post has been edited by Skydiver: 08 March 2013 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Determining if a string contains numbers

Posted 08 March 2013 - 11:29 AM

If you are that worried about the Regex set-up time, you should use a for loop rather than a foreach in your HasNumbers. You really won't notice much of a difference between any of the methods unless you are doing them in the million+ number of times.

Some timing tests: 10,000,000 runs each:
                        No     Starts     Ends     Middle      All
Regex.IsMatch        11,919     6,150    13,499    10,346     6,862
Predefined Regex      8,564     1,982     9,230     7,043     2,341
Precompiled Regex     7,152     1,282     8,114     4,550     1,216
foreach                 390        19       401       228        19
for                       8         8         8         8         8



Edit: Forgot to tell what each column is

All test strings were 26 characters long
No: no numbers
Starts: first characters is a number
Ends: last character is a number
Middle: 14th character is a number
All: all characters are numbers

This post has been edited by Momerath: 08 March 2013 - 11:31 AM

Was This Post Helpful? 2
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Determining if a string contains numbers

Posted 08 March 2013 - 11:39 AM

I was expecting some overhead with the foreach(), but not quite the 50x overhead if it's the last is a number or if there were no numbers. for(), I'm coming back to you. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1