views:

132

answers:

3

Hi, 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);

but the string remains the same. What's the reason ?

+15  A: 

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.

desco
+6  A: 

Because Trim() will remove any character in CharsToDelete at the beginning and at the end of the string.

You should use val.Replace() function.

DrakeVN
A: 

The correct way to do this would be

string val = 555*324-000
char[] CharsToDelete = {'*', '(', ')', '-', '[', ']', '{', '}' };

foreach (char c in CharsToDelete)
{
  val = val.Replace(c, '');
}
Xander
-1: The two overloads of `Replace()` are `Replace(string, string)` and `Replace(char, char)`. Neither accept a char array.
ck
Apologies... you are quite correct... i didnt fire up VS to check my mistakeNow fixed
Xander