Candor.StringExtensions.ReplaceNotIn C# (CSharp) Method

ReplaceNotIn() public static method

Replaces all characters in a text string that are not in a given list.
public static ReplaceNotIn ( this text, IList chars, String replacement ) : string
text this The text to be scanned.
chars IList The characters to be kept, such as a LexicalCharacterSet characters list.
replacement String The string to put in place of each character not in 'chars'.
return string
        public static string ReplaceNotIn(this string text, IList<char> chars, String replacement)
        {
            if (string.IsNullOrWhiteSpace(text))
                return text == null ? null : string.Empty;

            var newText = new StringBuilder(text.Length * replacement.Length);
            for (var i = 0; i < text.Length; i++)
            {
                if (chars.Contains(text[i]))
                    newText.Append(text[i]);
                else
                    newText.Append(replacement);
            }
            return newText.ToString();
        }