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. Getting hashtags from a string

Getting hashtags from a string

Scheduled Pinned Locked Moved C#
tutorialdata-structureshelp
6 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.
  • F Offline
    F Offline
    Farhad Eft
    wrote on last edited by
    #1

    Hi, I have a string that may contains several hashtags starting with # like this example: "Hello, I am #Hashtag1 and #Hashtag2" I want to save those hashtags, as many as they are, in an array. Can anyone help me how to do it. Thank you in advance.

    OriginalGriffO 1 Reply Last reply
    0
    • F Farhad Eft

      Hi, I have a string that may contains several hashtags starting with # like this example: "Hello, I am #Hashtag1 and #Hashtag2" I want to save those hashtags, as many as they are, in an array. Can anyone help me how to do it. Thank you in advance.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Use a regex to identify them:

      \#\w*

      You can then use the Regex.Matches to convert them to a collection with Linq methods:

      string[] hashtags = Regex.Matches(myDate, @"\#\w*").Cast().Select(m => m.Value).ToArray();

      [edit]Matches, not Match, Griff :sigh:[/edit]

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      Richard DeemingR F 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        Use a regex to identify them:

        \#\w*

        You can then use the Regex.Matches to convert them to a collection with Linq methods:

        string[] hashtags = Regex.Matches(myDate, @"\#\w*").Cast().Select(m => m.Value).ToArray();

        [edit]Matches, not Match, Griff :sigh:[/edit]

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        That should be Regex.Matches, not Regex.Match.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        OriginalGriffO 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          That should be Regex.Matches, not Regex.Match.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Clearly, I read what I meant to type... :doh: Thanks!

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Use a regex to identify them:

            \#\w*

            You can then use the Regex.Matches to convert them to a collection with Linq methods:

            string[] hashtags = Regex.Matches(myDate, @"\#\w*").Cast().Select(m => m.Value).ToArray();

            [edit]Matches, not Match, Griff :sigh:[/edit]

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            F Offline
            F Offline
            Farhad Eft
            wrote on last edited by
            #5

            thank you so much, it worked perfectly :)

            OriginalGriffO 1 Reply Last reply
            0
            • F Farhad Eft

              thank you so much, it worked perfectly :)

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              You're welcome!

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              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