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.

Recommended Answers

All 3 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.