Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Compare string and int

Compare string and int

Scheduled Pinned Locked Moved C#
jsonquestiondiscussion
7 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    szukuro
    wrote on last edited by
    #1

    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?

    G Mircea PuiuM CPalliniC T 4 Replies Last reply
    0
    • S szukuro

      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?

      Mircea PuiuM Offline
      Mircea PuiuM Offline
      Mircea Puiu
      wrote on last edited by
      #2

      You do not have to worry about exceptions if you use Int32.TryParse(). I would go for integer comparison.

      SkyWalker

      1 Reply Last reply
      0
      • S szukuro

        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?

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        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);

        S 1 Reply Last reply
        0
        • S szukuro

          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?

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          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 an int 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 :-D

          If 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.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • G Guffa

            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);

            S Offline
            S Offline
            szukuro
            wrote on last edited by
            #5

            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

            G 1 Reply Last reply
            0
            • S szukuro

              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?

              T Offline
              T Offline
              Thomas Stockwell
              wrote on last edited by
              #6

              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[^]

              1 Reply Last reply
              0
              • S szukuro

                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

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                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);

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups