views:

569

answers:

5

I have the following variables:

string str1 = "1";
string str2 = "asd";
string str3 = "3.5";
string str4 = "a";

Now I need to find the data type of each string i.e. the data type to which it can be converted if quotes are removed. Here is what I would like each variable to convert to:

str1 - integer
str2 - string
str3 - double
str4 - char

Note: if the string has single character it should be char, though a string can have single letter, I'm limiting it.

FYI: these values are obtained from DataGrid where i manually entered values. So everything is becoming a string. Is there any way to do this?

+3  A: 

Use the TryParse method of each type.

Mehrdad Afshari
In that case, "1" = Decimal, Int16, Int32, Int64, Double...
ck
ck, is there a problem? :)
Mehrdad Afshari
@ck: just prioritize the attempts: start narrow (int) and work towards broader types (double).
Joel Coehoorn
+3  A: 

There is no built in way to do this, you could attempt TryParse on number types with increasing precision, but it wouldn't guarantee it to be right.

Your best bet what be to process it like you would manually. i.e. Is there a decimal place? No - then its an integer. How big? Is it negative?

ck
+3  A: 

Of course, there's no definite way to do this, but if you create a list of data types you want to check ordered by priority, then something like this may do the trick.

object ParseString(string str)
{
    int intValue;
    double doubleValue;
    char charValue;
    bool boolValue;

    // Place checks higher if if-else statement to give higher priority to type.
    if (int.TryParse(str, out intValue))
        return intValue;
    else if (double.TryParse(str, out doubleValue))
        return intValue;
    else if (char.TryParse(str, out charValue))
        return intValue;
    else if (bool.TryParse(str, out boolValue))
        return intValue;

    return null;
}

Just call this function on each string, and you should have the appropiate type of object returned. A simple type check can then tell you how the string was parsed.

Noldorin
+3  A: 

The datatype for each of these items is string. If you want to attempt to parse them into different types you can use Int32.TryParse, Double.TryParse, etc. Or you can use Regex:

bool isInt = new Regex(@"^\d+$").IsMatch(str);
bool isDouble = !(isInt) && new Regex(@"^\d+\.\d+$").IsMatch(str);
bool isChar = !(isInt || isDouble) && new Regex(@"^.$").IsMatch(str);
bool isString = !(isInt || isDouble || isChar);
Richard Szalay
isChar should be new Regex(@"^.$");
configurator
Good point, though I wasn't checking if the previous matches failed. Also, I wasn't even calling IsMatch ;)
Richard Szalay
The TryParse methods are the way to go. You really don't want RegEx for this sort of thing (even ignoring the issue of speed).
Noldorin
What happens when regional settings have a comma as a decimal point?
Ilia Jerebtsov
thanx a lot. but how can i include negative values for integer.?
pragadheesh
+6  A: 

Use meta-data, if you can

That you have to guess what the data types are, is not a good idea.

Two things

1 Where is the data coming from?

If it's a database, are you sure they're strings? If it is a database, there should be some meta data returned that will tell you what the datatypes of the fields are.

If it's an Xml file, is there a schema defined that will give you the types?

2 If you have to continue to guess.

Be aware that you can have strings that happen to be numbers, but are perfectly valid strings e.g phone numbers, bank acount numbers, that are best expressed as strings. Also these numbers can have many digits, if you convert them to doubles you may loose some digits to floating point inaccuracies (you should be OK up to 14 or 15 digits)

I'm sure by now - cause I've taken my time typing this - there are lots of answers telling you how to do this (i.e. tryparse int first, then double, then test length for char, if not then it's a string etc), but if I were you, I'd try to NOT do that, and see if there's any way you can get, or pass some meta-data that will tell you what type it IS and not just what type it might be

Binary Worrier
+1 Guessing data types usually means that there's something wrong on a different level.
Richard Szalay
+1 Don't try to guess a type unless you really really really... really need to.
Coincoin
FYI: these values are obtained from DataGrid where i manually entered values. So everything is becoming a string.
pragadheesh
also i used a separate method to check the boundary values of data types.
pragadheesh
Why not bind the grid to a strongly typed data source (it can just be a collection of objects), or you can specify different types for each column in the grid definition (I think, I'm not up on data grids to be honest)
Binary Worrier