c# string Trim doesn't work, a weird problem
I have string like that:
string val = 555*324-000
now, I need to remove * and - chars, so I use that code (based on MSDN)
char[] CharsToDelete = {'*', '(', ')', '-', '[', ']', '{', '}' };
string result = val.Trim(CharsToDelete);
开发者_如何学Python
but the string remains the same. What's the reason ?
Trim ...Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object. You should use Replace method instead.
Because Trim() will remove any character in CharsToDelete at the beginning and at the end of the string.
You should use val.Replace() function.
The correct way to do this would be
string val = 555*324-000
char[] CharsToDelete = {'*', '(', ')', '-', '[', ']', '{', '}' };
foreach (char c in CharsToDelete)
{
val = val.Replace(c, '');
}
精彩评论