private static string StripInvalidCharacters(string value)
{
if (String.IsNullOrWhiteSpace(value))
{
// The case where the value is null or whitespace is treated as a special case
// of a string with no invalid characters and hence the consistent return type
// for other input of this type is to return the empty string.
return String.Empty;
}
var builder = new StringBuilder(value.Length);
var nextMustBeStartChar = true;
foreach (var c in value)
{
if (c == '.')
{
if (!nextMustBeStartChar)
{
builder.Append(c);
}
continue;
}
switch (Char.GetUnicodeCategory(c))
{
case UnicodeCategory.UppercaseLetter:
case UnicodeCategory.LowercaseLetter:
case UnicodeCategory.TitlecaseLetter:
case UnicodeCategory.ModifierLetter:
case UnicodeCategory.OtherLetter:
case UnicodeCategory.LetterNumber:
{
nextMustBeStartChar = false;
builder.Append(c);
break;
}
case UnicodeCategory.NonSpacingMark:
case UnicodeCategory.SpacingCombiningMark:
case UnicodeCategory.DecimalDigitNumber:
case UnicodeCategory.ConnectorPunctuation:
if (!nextMustBeStartChar)
{
builder.Append(c);
}
break;
}
}
return builder.ToString();
}