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. 'Comparing' two strings.

'Comparing' two strings.

Scheduled Pinned Locked Moved C#
help
5 Posts 3 Posters 0 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.
  • H Offline
    H Offline
    heelios
    wrote on last edited by
    #1

    Hello, I am facing a little problem with strings. In my application, I need to make sure that a long string, let's say "Hello world, I am currently on CodeProject." contents a small string, lets say "CodeProject". I'd like to find a function or a way to return true if the small string is in the large string and false if it isn't present. Thank you for your help. - Heel

    J T 2 Replies Last reply
    0
    • H heelios

      Hello, I am facing a little problem with strings. In my application, I need to make sure that a long string, let's say "Hello world, I am currently on CodeProject." contents a small string, lets say "CodeProject". I'd like to find a function or a way to return true if the small string is in the large string and false if it isn't present. Thank you for your help. - Heel

      J Offline
      J Offline
      Jeff Varszegi
      wrote on last edited by
      #2

      It's easy to write a function for this, but a better idea is to get in the habit of doing things in a standard way. The String class has a method called IndexOf that lets you get the first occurrence of a substring; that's how people usually accomplish your task. So this

      if (myString.IndexOf(mySubstring) >= 0)
      {
      }

      is better than a hand-rolled function because it's faster and it's standard, which means easy to understand at a glance. - Jeff

      H 1 Reply Last reply
      0
      • J Jeff Varszegi

        It's easy to write a function for this, but a better idea is to get in the habit of doing things in a standard way. The String class has a method called IndexOf that lets you get the first occurrence of a substring; that's how people usually accomplish your task. So this

        if (myString.IndexOf(mySubstring) >= 0)
        {
        }

        is better than a hand-rolled function because it's faster and it's standard, which means easy to understand at a glance. - Jeff

        H Offline
        H Offline
        heelios
        wrote on last edited by
        #3

        Many thanks.

        J 1 Reply Last reply
        0
        • H heelios

          Hello, I am facing a little problem with strings. In my application, I need to make sure that a long string, let's say "Hello world, I am currently on CodeProject." contents a small string, lets say "CodeProject". I'd like to find a function or a way to return true if the small string is in the large string and false if it isn't present. Thank you for your help. - Heel

          T Offline
          T Offline
          TigerNinja_
          wrote on last edited by
          #4

          Jeff's solution is recommended for your case, but if you ever need a bit more power (pattern matching) you may want to look into regular expressions. http://www.ondotnet.com/pub/a/dotnet/2002/03/11/regex2.html[^] HTH


          R.Bischoff .NET, Kommst du mit?
          Great Freeware -> Abilon - Rss Reader | Zip Genius | SmartFTP

          1 Reply Last reply
          0
          • H heelios

            Many thanks.

            J Offline
            J Offline
            Jeff Varszegi
            wrote on last edited by
            #5

            You're welcome!

            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