Hi,

I'm trying to trim certain caracters from a string but it is not working.

Here is my code:

string sMystring = "what you waiting for? ";
char[] cInvalid = { '\\', '-', '/', '*', '?', '<', '>', '|'}; 
sMystring = sMystring.Trim(cInvalid);

I realized that if I remove the last space in the string it works, but why is this happening?

It seems to me that the last white space should not interfere in the process.

Thanks in advance.

Edited 5 Years Ago by MrCapuchino: n/a

It works if you add a space to your char array

char[] cInvalid = { '\\', '-', '/', '*', '?', '<', '>', '|', ' '};

I guess Trim defaults to whitespace trimming if it can't find a char to trim in the cIvalid array.

Edited 5 Years Ago by ddanbe: n/a

Since Trim only removes from the beginning and end, the whitespace character at the end doesn't match anything in your char array so it's done trimming. If you are trying to remove characters from the string, then you need to use the Remove method.

Ok, Thank you for your answer, I thought Trim remove any caracter not just the beginning and end.

Thanks

This question has already been answered. Start a new discussion instead.