Using regular expressions to get initials from a string in C#

Here is a really simple string extension to help you get initials from a string in C# public static string ToInitials(this string str) { return Regex.Replace(str, @”^(?’b’w)w*,s*(?’a’w)w*$|^(?’a’w)w*s*(?’b’w)w*$”, “${a}${b}”, RegexOptions.Singleline) } Here’s a breakdown on what the expression is doing: ^ matches beginning of the string (?‘b’w) captures first character of a word and stores in …

Using regular expressions to get initials from a string in C# Read More »