0

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 by MrCapuchino: n/a

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by MrCapuchino
0

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 by ddanbe: n/a

0

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.

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.