stringByTrimmingCharactersInSet: is not removing characters in the middle of the string

Sookcha picture Sookcha · Apr 7, 2011 · Viewed 21.4k times · Source

I want to remove "#" from my string.

I have tried

 NSString *abc = [@"A#BCD#D" stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"#"]];

But it still shows the string as "A#BCD#D"

What could be wrong?

Answer

Vladimir picture Vladimir · Apr 7, 2011

stringByTrimmingCharactersInSet removes characters from the beginning and end of your string, not from any place in it

For your purpose use stringByReplacingOccurrencesOfString:withString: method as others pointed.