Hi, I would like to convert a word, which in the right case, into a sentence with a name. Example:
NumberOfLoyalHonestWomen will become
The number of faithful honest women
Basically, this method is combined with reflection to turn field / property names into people's shortcuts when they automatically generate inputs for the screen.
Here is what I got. Is there a better or cleaner way?
Dot net fiddle
using System;
using System.Text.RegularExpressions;
using System.Linq;
public class Program
{
public static void Main()
{
string testString = "ATCPConnection";
Console.WriteLine(testString.ToSentence());
}
}
public static class StringExtensions
{
public static string ToSentence(this string Source)
{
return Regex.Replace(string.Concat(from Char c
in Source.ToCharArray()
select Char.IsUpper(c) ? " " + c : c.ToString()).Trim(),
"(?<AcronymLetter>[A-Z]) (?=[A-Z](?![a-z]))",
"${AcronymLetter}");
}
}
Side note. This was complicated by my desire to keep the acronyms intact, hence <<22>. For instance:
MGTOWSaysThereAreMoreUnicorns will become
MGTOW says there are more unicorns
source
share