4

I am trying to check if a string contains a numeric value, if it doesnt return to a label if it does then I would like to show the main window. How can this be done?

If (mystring = a numeric value)
            //do this:
            var newWindow = new MainWindow();
            newWindow.Show();
If (mystring = non numeric)
            //display mystring in a label
            label1.Text = mystring;

else return error to message box
1
  • 2
    does non-numeric mean no numbers, or just "not all numbers"? Commented Apr 25, 2012 at 14:40

7 Answers 7

6

use TryParse.

double val;
if (double.TryParse(mystring, out val)) {
    ..
} else { 
    ..
}

This will work for strings that translate directly to a number. If you need to worry about stuff like $ and , also, then you'll need to do a little more work to clean it up first.

Sign up to request clarification or add additional context in comments.

Comments

5
Int32 intValue;
if (Int32.TryParse(mystring, out intValue)){
  // mystring is an integer
}

Or, if it's a decimal number:

Double dblValue;
if (Double.TryParse(mystring, out dblValue)){
  // mystring has a decimal number
}

Some examples, BTW, can be found here.

Testing foo:
Testing 123:
    It's an integer! (123)
    It's a decimal! (123.00)
Testing 1.23:
    It's a decimal! (1.23)
Testing $1.23:
    It's a decimal! (1.23)
Testing 1,234:
    It's a decimal! (1234.00)
Testing 1,234.56:
    It's a decimal! (1234.56)

A couple more I've tested:

Testing $ 1,234:                      // Note that the space makes it fail
Testing $1,234:
    It's a decimal! (1234.00)
Testing $1,234.56:
    It's a decimal! (1234.56)
Testing -1,234:
    It's a decimal! (-1234.00)
Testing -123:
    It's an integer! (-123)
    It's a decimal! (-123.00)
Testing $-1,234:                     // negative currency also fails
Testing $-1,234.56:

5 Comments

so double doesnt check if its just one number only a decimal?
As an interesting aside, Double.TryParse can handle commas in a number but Int32.TryParse cannot.
Never knew that about the commas. Seems like a strange design choice since as a 000's separator they are equally valid or invalid in floating point and integer numbers. If anything, it would make more sense to only allow them in integers since there's a culture-specific ambiguity with FP numbers! Really odd.
@jamietre: Yes, it is indeed odd. I was also surprised, when I kept going, that negative currency also fails. Why they accept one method and not the next is...odd.
@Kirsty: if you only care if it's a number then use double.TryParse as an integer is also a valid double. If you require that the number also be an integer, then use int.TryParse
2
double value;
if (double.tryParse(mystring, out value))
{
        var newWindow = new MainWindow();
        newWindow.Show();
}
else
{
    label1.Text = mystring;
}

Comments

0

You could simply reference Microsoft.VisualBasic.dll, then do the following:

if (Microsoft.VisualBasic.Information.IsNumeric(mystring))
{
    var newWindow = new MainWindow();
    newWindow.Show();
}
else
{
    label1.Text = mystring;
}

The VB one actually performs better, as it doesn't throw an exception for every failed conversion.

See: Exploring IsNumeric for C#

6 Comments

using the VB library is sort of interesting, but that article must have been written before TryParse. There's no reason to use try/catch to test something for being numeric in C#.
Yea, I'm sure it was. It is an interesting solution though and that's why I thought I'd share it (as being a VB guy myself, I use it all the time).
Honestly I had never even given it a thought before. I probably would be hesitant to do so in anything more than a one-off just because it's another dependency, but it's a tool in your toolbox to be sure.
I wouldn't reference VB to check if a string is numeric or not. there are so many other ways X.tryParse, Regex, LINQ, foreach some are better than the others, ** but there is just no need to use C# little brother - VB.** for this task(or should I say for tasks.)
@gdoron - It's simply an alternative solution. It all comes down to preference.
|
0

You may use a boolean to judge if the string contains numeric chars.

public bool GetNumberFromStr(string str)
    {
        string ss = "";
        foreach (char s in str)
        {
            int a = 0;
            if (int.TryParse(Convert.ToString(s), out a))
                ss += a;
        }
        if ss.Length >0
           return true;
        else
           return false;
    } 

1 Comment

Using TryParse(Convert...)) is about the most inefficient way I can think of to test a char for whether or not it's a digit.
0

Try taking the Label's caption into a string and use Int.TryParse() method to decide whether text is integer or string. the method will return true if yes , else it will return false. Code will be like :

if (Int.TryParse(<string> , out Num) == True)
{
   // is numeric
}
else
{
   //is string
}   

where Num will contain your integer value if conversion is a success

Comments

0

A good example of method that will do this can be found at: http://msdn.microsoft.com/en-us/library/f02979c7.aspx

There's even some code doing something almost exactly what you want. You can use a Int32.TryParse(string) if you're expecting integer values. If you anticipate doubles, use Double.TryParse(string).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.