Getting hashtags from a string
-
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.
-
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.
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...
-
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...
That should be
Regex.Matches
, notRegex.Match
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
That should be
Regex.Matches
, notRegex.Match
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Clearly, I read what I meant to type... :doh: Thanks!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
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...
thank you so much, it worked perfectly :)
-
thank you so much, it worked perfectly :)
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...