Code:
NSString *str = @"ABC\rDEF";
str = [str stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"\r"]];
NSLog(@"%s", [str UTF8String]);
Result
ABC
DEF
NSString *str = @"ABC\rDEF";
str = [str stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"\r"]];
NSLog(@"%s", [str UTF8String]);
ABC
DEF
Check the NSString
class reference reference for stringByTrimmingCharactersInSet:
:
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
Since the \r
occurs in the middle of the string, noting happens.
Anne answer is correct trimming will not impact a character that is not at the beginning or end of a string. You want to do a replace of all occurrences of \r, so you should use stringByReplacingOccurrencesOfString
Here is another answer that covers this problem: stringByTrimmingCharactersInSet: is not removing characters in the middle of the string