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. Searching for words in a string

Searching for words in a string

Scheduled Pinned Locked Moved C#
questionalgorithms
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.
  • A Offline
    A Offline
    Azel Low
    wrote on last edited by
    #1

    Is there a fast way of finding the number of times a word appears in a string? Eg. String: "How do I search if a words exist in a string" Target Word: "a" Result: 2

    V 1 Reply Last reply
    0
    • A Azel Low

      Is there a fast way of finding the number of times a word appears in a string? Eg. String: "How do I search if a words exist in a string" Target Word: "a" Result: 2

      V Offline
      V Offline
      V ktor
      wrote on last edited by
      #2

      Does this meet your needs? string str = "How do I search if a words exist in a string"; MatchCollection mc = Regex.Matches(str, " a "); Console.WriteLine(mc.Count.ToString()); or perhaps... string str = "How do I search if a words exist in a string"; int index = str.IndexOf(" a "); int numWords = 0; while(index != -1) { index = str.IndexOf(" a ", index + 1); numWords++; } Console.WriteLine(numWords.ToString());

      A 1 Reply Last reply
      0
      • V V ktor

        Does this meet your needs? string str = "How do I search if a words exist in a string"; MatchCollection mc = Regex.Matches(str, " a "); Console.WriteLine(mc.Count.ToString()); or perhaps... string str = "How do I search if a words exist in a string"; int index = str.IndexOf(" a "); int numWords = 0; while(index != -1) { index = str.IndexOf(" a ", index + 1); numWords++; } Console.WriteLine(numWords.ToString());

        A Offline
        A Offline
        Azel Low
        wrote on last edited by
        #3

        V|ktor wrote: string str = "How do I search if a words exist in a string"; MatchCollection mc = Regex.Matches(str, " a "); Console.WriteLine(mc.Count.ToString()); Thank You! This worked great for me. Here's a 5. ;)

        M 1 Reply Last reply
        0
        • A Azel Low

          V|ktor wrote: string str = "How do I search if a words exist in a string"; MatchCollection mc = Regex.Matches(str, " a "); Console.WriteLine(mc.Count.ToString()); Thank You! This worked great for me. Here's a 5. ;)

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          Just a little addition: V|ktor's solution will find the word only if it's surrounded by spaces. If you use @"\ba\b" instead, then the RE will match 'a' at the beginning or the end of the string, too. If you haven't done yet I suggest you read through this excellent tutorial: The 30 Minute Regex Tutorial [^] Regards, mav

          A 1 Reply Last reply
          0
          • M mav northwind

            Just a little addition: V|ktor's solution will find the word only if it's surrounded by spaces. If you use @"\ba\b" instead, then the RE will match 'a' at the beginning or the end of the string, too. If you haven't done yet I suggest you read through this excellent tutorial: The 30 Minute Regex Tutorial [^] Regards, mav

            A Offline
            A Offline
            Azel Low
            wrote on last edited by
            #5

            Thanks mav.northwind! :) I was just testing V|ktor's code and was wondering abt Casing and the Spaces too.

            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