The following sourcecode can strip charaters from string. For example: removing "F9" from "FRYAN0911" will give you "RYAN011"
public static string RemoveChars(string s, string myChars)
{
int i = 0, j = 0;
int CharLength = myChars.Length;
int StringLength = s.Length;
int[] intCollection = new int[256];
char[] s2 = new char[StringLength];
for (i = 0; i < CharLength; i++)
{
intCollection[myChars[i]] = 1;
}
i = j = 0;
for (i = 0; i < StringLength; i++)
{
if (intCollection[s[i]] != 1)
{
s2[j] = s[i];
j++;
}
}
return new string(s2);
}
For more C# coding tips, subscribe now
3 comments:
http://awesvb.blogspot.com/2009/06/remove-embedded-dashes-from-number.html
Why not use string.Replace()?
Because string.Replace() can only strip 1 character at a time. How about if you need to remove 20 different character in a string? You will have to call string.Replace() 20x.
Post a Comment