Compare string and int
-
Greetings! I have two fields, one int and one string. The string stores an integer value. I have to check these fields for equality. I don't know if I should compare them as integers or as strings. I'm more for string comparison, since then I don't have to worry about getting an exception (as opposed to using Int32.Parse), so I don't need to use a try-catch block. Then again, the string gets its value as someint.ToString(), so parsing it shouldn't throw any Exceptions. Also I think comparing two integers should be a bit faster then is the case with strings. Any thoughts on the subject?
-
Greetings! I have two fields, one int and one string. The string stores an integer value. I have to check these fields for equality. I don't know if I should compare them as integers or as strings. I'm more for string comparison, since then I don't have to worry about getting an exception (as opposed to using Int32.Parse), so I don't need to use a try-catch block. Then again, the string gets its value as someint.ToString(), so parsing it shouldn't throw any Exceptions. Also I think comparing two integers should be a bit faster then is the case with strings. Any thoughts on the subject?
You do not have to worry about exceptions if you use Int32.TryParse(). I would go for integer comparison.
SkyWalker
-
Greetings! I have two fields, one int and one string. The string stores an integer value. I have to check these fields for equality. I don't know if I should compare them as integers or as strings. I'm more for string comparison, since then I don't have to worry about getting an exception (as opposed to using Int32.Parse), so I don't need to use a try-catch block. Then again, the string gets its value as someint.ToString(), so parsing it shouldn't throw any Exceptions. Also I think comparing two integers should be a bit faster then is the case with strings. Any thoughts on the subject?
You should of course not store an integer as a string in the first place, but I assume that you know that, and that the storage is out of your control... Comparing integers are of course faster than comparing strings, but I'm not sure that the actual comparison is a large enough part of the entire operation for that to make any real difference. To compare the values as strings you have to be absolutely positive that the format of the strings are always exactly the same, as the strings "42", " 42", "042", "42.0" and "0x2a" won't be equal although they contain the same value.
--- Year happy = new Year(2007);
-
Greetings! I have two fields, one int and one string. The string stores an integer value. I have to check these fields for equality. I don't know if I should compare them as integers or as strings. I'm more for string comparison, since then I don't have to worry about getting an exception (as opposed to using Int32.Parse), so I don't need to use a try-catch block. Then again, the string gets its value as someint.ToString(), so parsing it shouldn't throw any Exceptions. Also I think comparing two integers should be a bit faster then is the case with strings. Any thoughts on the subject?
szukuro wrote:
I'm more for string comparison, since then I don't have to worry about getting an exception
if the
string
must contain a value representing anint
then you have to worry about. :) Moreover, if you compare strings maybe you don't get what you expect, for instance:" 15"
is not equal to"15"
. Cheers :-DIf the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
You should of course not store an integer as a string in the first place, but I assume that you know that, and that the storage is out of your control... Comparing integers are of course faster than comparing strings, but I'm not sure that the actual comparison is a large enough part of the entire operation for that to make any real difference. To compare the values as strings you have to be absolutely positive that the format of the strings are always exactly the same, as the strings "42", " 42", "042", "42.0" and "0x2a" won't be equal although they contain the same value.
--- Year happy = new Year(2007);
As I said before, the string is actually someint.ToString(). To be more precise, I'm actually getting a hidden field value in ASP.NET, which is of course a string. Since I set the value of the hidden field (on the client side), I also know the format, which happens to be the plain "42" type. Obviously in this case favoring one comparison over another doesn't actually make a difference at all. I was just curious if someone knows a best practice for this kind of problem. Perhaps in the future I will come across a similiar situation, where the differences in comparison time do matter. It's just like those someString.Length > 0 vs. someString != String.Empty questions. [edit]Actually the problem is pretty much like the problem I mentioned as an example, as I am willing to except a third choice, if possible, like String.IsNullOrEmpty(someString) is for the example above :)[/edit] -- modified at 9:33 Friday 12th January, 2007
-
Greetings! I have two fields, one int and one string. The string stores an integer value. I have to check these fields for equality. I don't know if I should compare them as integers or as strings. I'm more for string comparison, since then I don't have to worry about getting an exception (as opposed to using Int32.Parse), so I don't need to use a try-catch block. Then again, the string gets its value as someint.ToString(), so parsing it shouldn't throw any Exceptions. Also I think comparing two integers should be a bit faster then is the case with strings. Any thoughts on the subject?
Well if the two strings can be the same and then the integers of the strings will be the same. But if you use lets say: Convert.ToInt32(string val) part of the System? namespace. If the ints are the same then the strings should be the same. The may be some circumstances or possibly exceptions if the string you are trying to convert has letters and numbers in them. string a, b; a="123" b="123" a equals b - when comparing the string values Convert.ToInt32(a) equals Convert.ToInt32(b) - when comparing the integers of the string a="L123" b="L123" a equals b - when comparing the string values Convert.ToInt32(a) does not equal Convert.ToInt32(b) - will probably cause an exception or it may convert the letter 'L' to asc, I'm not real sure
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]
-
As I said before, the string is actually someint.ToString(). To be more precise, I'm actually getting a hidden field value in ASP.NET, which is of course a string. Since I set the value of the hidden field (on the client side), I also know the format, which happens to be the plain "42" type. Obviously in this case favoring one comparison over another doesn't actually make a difference at all. I was just curious if someone knows a best practice for this kind of problem. Perhaps in the future I will come across a similiar situation, where the differences in comparison time do matter. It's just like those someString.Length > 0 vs. someString != String.Empty questions. [edit]Actually the problem is pretty much like the problem I mentioned as an example, as I am willing to except a third choice, if possible, like String.IsNullOrEmpty(someString) is for the example above :)[/edit] -- modified at 9:33 Friday 12th January, 2007
Well, if you only have one string value and a lot of integers, then you should obviously convert the string to an integer. Look at the number of string operations that you have to do to using different methods. Integer comparisons are so cheap in comparison that they can be ignored. If you convert the string to an integer, you have a single string operation. If you convert the integers to strings, you will have one string operation per value. If you on the other hand had a single integer value to compare to a lot of strings: If you convert each string to an integer and the compare the integers, you have one string operation per value. If you convert the integer value to a string and then compare that to each string, you have one string operation per value plus an additional string operation. In this case the methods will cost about the same.
--- Year happy = new Year(2007);