0

Code:

NSString *str = @"ABC\rDEF";

str = [str stringByTrimmingCharactersInSet:
        [NSCharacterSet characterSetWithCharactersInString:@"\r"]];
NSLog(@"%s", [str UTF8String]);

Result

ABC
DEF
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Sean
  • 9
  • 4

2 Answers2

3

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
  • 26,765
  • 9
  • 65
  • 71
0

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

Community
  • 1
  • 1
jfno
  • 39
  • 5