Hi i have some text which is meant to be displayed within a label.
“Hey @ronald and @tom where are we going this weekend”
What i need to do is change it to this instead
“Hey http://www.domain.com/ronald and http://www.domain.com/tom where are we going this weekend”
Now i have this code which someone off stackoverflow has helped me construct, however i am lost on what to do in the next step.
Regex regex = new Regex(@"@[\S]+");
MatchCollection matches = regex.Matches(strStatus);
foreach (Match match in matches)
{
string Username = match.ToString().Replace("@", "");
}
I cannot set the label in the foreach because it would disregard the last word replaced, i hope i am making sense.
Keep the usernames you find in a list. Iterate over these from longest to shortest, replacing each occurrence of @[username] with http://www.domain.com/%5Busername%5D. The reason to do longest to shortest is to avoid replacing partial matches, as in “Hey, @tom and @tomboy …” This certainly isn’t the most efficient way to do the replacement (since you do a full string scan for each username, but given your example I suspect your strings are short and the lack of efficiency weighs less than the simplicity of this mechanism.
If you want to construct actual links it would look like: