C # string Trim not working, weird problem

I have a line like this:

string val = 555*324-000

now I need to remove the * and - characters, so I use this code ( based on MSDN )

char[] CharsToDelete = {'*', '(', ')', '-', '[', ']', '{', '}' };
string result = val.Trim(CharsToDelete);

but the string remains unchanged. What reason?

+3
source share
3 answers

Trim ... Removes all leading and trailing occurrences of the character set specified in the array from the current String object. Instead, you should use a replacement method.

+19
source

Since Trim () will delete any character in CharsToDelete at the beginning and end of the line.

You should use the val.Replace () function.

+6
source

:

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

foreach (char c in CharsToDelete)
{
  val = val.Replace(c, '');
}
+1

Source: https://habr.com/ru/post/1754575/


All Articles