This C# function to remove any special characters in the input string which is not included in the allowed special character list.
/// <summary>
/// Removes any special characters in the input string not provided
/// in the allowed special character list.
///
/// Sometimes it is required to remove some special characters like
/// carriage return, or new line which can be considered as invalid
/// characters, especially while file processing. This method removes any
/// special characters in the input string which is not included in the
/// allowed special character list.
/// </summary>
/// <param name="input">Input string to process</param>
/// <param name="allowedCharacters">list of allowed special characters </param>
/// <returns>
/// The original string with special charactersremoved.
/// </returns>
/// <example>
///
/// Remove carriage return from the input string:
///
/// var processedString = RemoveSpecialCharacters(
/// "Hello! This is string to process. \r\n", @""",-{}.! "
/// );
///
/// </example>
/// <remarks>
/// http://extensionmethod.net/5470/csharp/string/removespecialcharacters
/// </remarks>
public static string RemoveSpecialCharacters(string input, string allowedCharacters)
{
char[] buffer = new char[input.Length];
int index = 0;
char[] allowedSpecialCharacters = allowedCharacters.ToCharArray();
foreach (char c in input.Where(
c => char.IsLetterOrDigit(c) || allowedSpecialCharacters.Any(
x => x == c)
))
{
buffer[index] = c;
index++;
}
return new string(buffer, 0, index);
}
// or...
/// <summary>
/// Remove any character that isn't [0-9], [A-Z], [a-z], [.] period, [-] hyphen, [_] underscore, or a [ ] space.
/// </summary>
/// <param name="text">The text to remove special characters from.</param>
/// <returns>The specified <paramref name="text"/> with special characters removed.</returns>
public static string RemoveSpecialCharacters(string text)
{
if (text == null)
{
return null;
}
var sb = new StringBuilder(text.Length);
foreach (var c in text)
{
if (
c >= '0' && c <= '9' ||
c >= 'A' && c <= 'Z' ||
c >= 'a' && c <= 'z' ||
c == '.' ||
c == '-' ||
c == '_' ||
c == ' '
)
{
sb.Append(c);
}
}
return sb.ToString();
}
// Unit test
[Theory]
[InlineData(@"!@#$%^&*()+=`~{[}]|\:;""'<>?/", "")]
[InlineData(@"Hello _world_. - Wilford Brimley", @"Hello _world_. - Wilford Brimley")]
[InlineData("", "")]
[InlineData(null, null)]
public void TestRemoveSpecialCharacters(string actual, string expected)
{
Assert.True(string.Equals(expected, StringUtil.RemoveSpecialCharacters(actual),
StringComparison.InvariantCulture));
}