Created
December 31, 2016 18:03
-
-
Save hclewk/2845f88f2d1042d0d2677da6fe735e36 to your computer and use it in GitHub Desktop.
Turn any string into a hashtag in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Input: "St. Patrick's Day: Awësöme tö-dö's" | |
//Output: "#StPatricksDayAwesomeToDos" | |
public static string MakeHashtag(string text) | |
{ | |
text = RemoveDiacritics(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text)); | |
text = Regex.Replace(text, @"[^a-zA-Z0-9]", ""); | |
return "#" + text; | |
} | |
/// See: http://www.siao2.com/2007/05/14/2629747.aspx | |
private static string RemoveDiacritics(string stIn) | |
{ | |
string stFormD = stIn.Normalize(NormalizationForm.FormD); | |
StringBuilder sb = new StringBuilder(); | |
for (int ich = 0; ich < stFormD.Length; ich++) | |
{ | |
System.Globalization.UnicodeCategory uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]); | |
if (uc != System.Globalization.UnicodeCategory.NonSpacingMark) | |
{ | |
sb.Append(stFormD[ich]); | |
} | |
} | |
return (sb.ToString().Normalize(NormalizationForm.FormC)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment