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. how to compare string in C#

how to compare string in C#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
4 Posts 3 Posters 18 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    strcmp?

    E 1 Reply Last reply
    0
    • L Lost User

      strcmp?

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      string s1 = "Hello"; string s2 = "Goodbye"; if (s1 == s2) { // strings are equal. }

      F 1 Reply Last reply
      0
      • E Eric Gunnerson msft

        string s1 = "Hello"; string s2 = "Goodbye"; if (s1 == s2) { // strings are equal. }

        F Offline
        F Offline
        fadi
        wrote on last edited by
        #3

        That does not work in C#. You need to do this: if (s1.CompareTo(s2)==0) { } 0 they are equal -1 s1 < s2 1 s1 > s2 check the help file

        E 1 Reply Last reply
        0
        • F fadi

          That does not work in C#. You need to do this: if (s1.CompareTo(s2)==0) { } 0 they are equal -1 s1 < s2 1 s1 > s2 check the help file

          E Offline
          E Offline
          Eric Gunnerson msft
          wrote on last edited by
          #4

          Comparison of strings in C# is value comparison. if I say: if (s1 == s2) that will be true if the strings have the same contents. If I want reference comparison, I need to use: if ((object) s1 == (object) s2) That forces the compiler to use object.Equals() rather than String.Equals(). In languages that don't support operator overloading, you'd use: if (String.Equals(s1, s2))

          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