If you don't need the entire string, you can take advantage of the delayed execution:
public static class StringExtensions
{
public static IEnumerable<char> RemoveChar(this IEnumerable<char> originalString, char removingChar)
{
return originalString.Where(@char => @char != removingChar);
}
}
You can even combine multiple characters...
string veryLongText = "abcdefghijk...";
IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
.RemoveChar('c')
.RemoveChar('d')
.Take(5);
... and only the first 7 characters will be evaluated :)
EDIT: or, even better:
public static class StringExtensions
{
public static IEnumerable<char> RemoveChars(this IEnumerable<char> originalString,
params char[] removingChars)
{
return originalString.Except(removingChars);
}
}
and its usage:
var veryLongText = "abcdefghijk...";
IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
.RemoveChars('c', 'd')
.Take(5)
.ToArray(); //to prevent multiple execution of "RemoveChars"