C # doubt searching data type

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 row, that is, the data type to which it can be converted if the quotation marks are removed. Here is what I would like for each variable to convert to:

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

Note: if a string has one character, it must be char, although a string can have one letter, I restrict it.

FYI: these values ​​are obtained from the DataGrid, where I manually entered the values. So everything becomes a string. Is there any way to do this?

+1
source share
5 answers

Use metadata if you can

What you have to guess which data types is not a good idea.

Two things

1 Where do the data come from?

If this is a database, are you sure these are strings? If it is a database, some metadata should be returned that will tell you what the data types are.

If it is an Xml file, is there a specific schema that will give you types?

2 If you need to keep on guessing.

Remember that you can have strings that are numbers, but are perfectly valid strings, such as phone numbers, bank account numbers, which are best expressed as strings. In addition, these digits can have many digits, if you convert them to doubles, you may lose some digits for floating point inaccuracies (you must be in order up to 14 or 15 digits)

I am sure of this - because I did not have time to type this word - there are many answers telling you how to do this (i.e. tryparse int first, then double, then test length for char, if not string, etc. etc.), but if I were you, I would try NOT to do this and see if there is any way that you can get, or pass some metadata that will tell you what type it is, not just the type which may be

+7
source

Of course, there is no definite way to do this, but if you create a list of data types that you want to check by priority, something like this might 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 doubleValue; else if (char.TryParse(str, out charValue)) return charValue; else if (bool.TryParse(str, out boolValue)) return boolValue; return null; } 

Just call this function for each row, and you should return the appropriate type of the returned object. A simple type check can tell you how the string was parsed.

+8
source

Use the TryParse method for each type.

+3
source

There is no built-in way to do this, you can try TryParse on types with increased precision, but this does not guarantee that this will be correct.

It is best to do what needs to be processed, as if you were manually. those. is there a decimal point? No - then this is an integer. How big Is it negative?

+3
source

The data type for each of these elements is a string. If you want to try 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); 
+3
source

All Articles