I have the following extension method (which is invalid and does not compile ATM):
public static string Strip( this string str, char[] charsToStrip )
{
foreach( char c in charsToStrip )
{
str.Replace( c, "" );
}
return str;
}
The str.Replace() call needs to invoke the ‘char’ overload of Replace(), however using this overload I’m not sure what to pass into the 2nd parameter to tell it to replace with “nothing”.
The goal of this function is to iterate each character in the charsToStrip variable and remove all instances of that character in the source string, str.
Also if my function is reinventing the wheel, let me know. I’m using .NET 3.5
Thanks in advance.
Try:
Note that instances of
stringare immutable. As such, you need to assign the result ofString.Replaceotherwise the result is lost.