Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

a have a string and i need to know if your value is a number. how i can do this?

C#
string myString = "1";
//is a string but your value is a number.


thanks
Posted

 
Share this answer
 
Comments
EduChapow 29-Jan-14 10:18am    
thank u!
Kornfeld Eliyahu Peter 29-Jan-14 10:22am    
You welcome...
Off the top of my head, you could always implement the brute force approach to testing the string entry - I've just knocked this up in Notepad but the theory is sound:
C#
public static class ConverterExtensions
{
  public static bool IsNumber(this string source)
  {
    if (string.IsNullOrWhitespace(source)) return false;

    const Types[] types = new Type[]{ 
      typeof(short), typeof(ushort), 
      typeof(int), typeof(uint), 
      typeof(long), typeof(ulong), 
      typeof(float), typeof(double), typeof(decimal)
    };
    foreach (Type type in types)
    {
      if (source.TryChangeType(type))
        return true;
    }
    return false;
  }

  public static bool TryChangeType(this string source, Type targetType)
  {
    try
    {
      Convert.ChangeType(source, targetType);
      return true;
    }
    catch (Exception)
    {
      return false;
    }
  }
}
 
Share this answer
 
Comments
EduChapow 29-Jan-14 10:18am    
nice
Pete O'Hanlon 29-Jan-14 10:21am    
Thanks. One of my big problems with the Convert class is it doesn't have a TryChangeType method - that's what this is for.
EduChapow 29-Jan-14 11:46am    
yes, extension methods is great!
C#
string myString = "1";
int intVal = 0;
if(int.TryParse(myString, out intVal))
{

}


C#
int.TryParse 
will return true if input value is integer and out paramter will have that integer value.
 
Share this answer
 
Comments
Pete O'Hanlon 29-Jan-14 10:17am    
The OP's requirement is to detect a number; not an integer. What happens if myString contains "1.2"?
EduChapow 29-Jan-14 10:18am    
thank you, i'm use it
Try this..

C#
int temp = 0;
       string myString = "1";
      bool isNumber =  int.TryParse(myString, out temp);
      if (isNumber)
      {
          // it is a number
      }
      else
      {
          // Not a number
      }
 
Share this answer
 
Comments
Pete O'Hanlon 29-Jan-14 9:32am    
What if it's a long? Or a double?
EduChapow 29-Jan-14 11:47am    
you can use long or double normaly, they have a extension method tryparse
Pete O'Hanlon 29-Jan-14 11:50am    
I know they do - my point is that you can't rely on int.TryParse if it can be any numeric type - you would potentially need to try all the different TryParse methods to find the relevant one that works so you will fall through the different ones when isNumber is false.
EduChapow 29-Jan-14 11:58am    
ohh yes, sure.
i'm does not using only int, but using all types like int16, 32, 64, double, long...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900