title: Find hashtags in string
body: <p>I am working on a Xamarin.Forms PCL project in C# and would like to detect all the hashtags. </p>
  
  <p>I tried splitting at spaces and checking if the word begins with an # but the problem is if the post contains two spaces like "Hello #World  Test" it would lose that the double space</p>
  
  <pre><code>string body = "Example string with a #hashtag in it";
          string newbody = "";
          foreach (var word in body.Split(' '))
          {
              if (word.StartsWith("#"))
                  newbody += "[" + word + "]";
              newbody += word;
          }
  </code></pre>
  
  <p>Goal output:</p>
  
  <blockquote>
    <p>Example string with a [#hashtag] in it</p>
  </blockquote>
  
  <p>I also only want it to have A-Z a-z 0-9 and _ stopping at any other character</p>
  
  <blockquote>
    <p>Test #H3ll0_W0rld$%Test => Test [#H3ll0_W0rld]$%Test</p>
  </blockquote>
  
  <p>Other Stack questions try to detect the string and extract it, I would like it work with it and put it back in the string without losing anything that methods such as splitting by certain characters would lose.</p>