-2

How to remove character '\' in NSString IOS

For example,

NSString *abc = @"bmcvn\nmsf;

I have tried:

NSString *stri = @"\rdffsdf";
NSString *str = [stri stringByReplacingOccurrencesOfString:@"\\" withString:@"123"];
NSLog(str);

but it didn't replace

jww
  • 97,681
  • 90
  • 411
  • 885
  • 3
    Have you looked at the `NSString` API? See any methods that could help? Tried any? Show what you tried and explain what didn't work. – Wain Jun 24 '14 at 11:12
  • i try NSString *stri = @"\rdffsdf"; NSString *str = [stri stringByReplacingOccurrencesOfString:@"\\" withString:@"123"]; NSLog(str); but it not work – user3094696 Jun 24 '14 at 11:15
  • 1
    Basically, `\\`, standing alone, isn't a real character, but is half of an encoded character. This is standard C stuff which you should know before diving into Objective-C. – Hot Licks Jun 24 '14 at 11:58
  • So, the first six answers were wrong, the people who provided the answers did not understand string escape sequences as simple as carriage return. This is a sad state of programmers and also probably indicates a lack of reading books on the basics of the "C" language. The languages are the most basic tools we have, not understanding them is like a carpenter not understanding to to use a hammer. – zaph Jun 24 '14 at 12:12
  • Sadly the five who marked the question as a duplicate did not catch that it is about an escaped character, not simple string replacement. I have flagged for reopen. – zaph Jun 24 '14 at 12:24

4 Answers4

2

You can't replace "\" in the string "\rdffsdf" because it is part the two character representation "\r" of the single character with the hex value 0x0d (13 decimal). It is the "carriage return" character.

When a carriage return character (0x0d) is needed in a string it is entered as "\r". Also common are the two character sequences line feed "\n" (0X0a), horizontal tab "\t" (0X09) and finally backslash "\" (0x5c).

The backslash case:

NSString *stri = @"\\rdffsdf";
NSString *str = [stri stringByReplacingOccurrencesOfString:@"\\" withString:@"123"];
NSLog(@"str: %@", str);

NSLog output:

str: 123rdffsdf

zaph
  • 111,848
  • 21
  • 189
  • 228
2

Note the difference between replacing the backslash char

NSString *original = @"foo\\bar";
NSLog(@"%@", original); // Prints: foo\bar
NSString *replaced = [original stringByReplacingOccurrencesOfString:@"\\" withString:@""];
NSLog(@"%@", replaced); // Prints: foobar

And replacing a char that is represented using a backslash (\r, \n, \t, ...):

NSString *original = @"thisIsCarriageReturn\rRightThere";
NSLog(@"%@", original); // Prints: thisIsCarriageReturn
                        //         RightThere
NSString *replaced = [original stringByReplacingOccurrencesOfString:@"\r" withString:@""];
NSLog(@"%@", replaced); // Prints: thisIsCarriageReturnRightThere
txulu
  • 1,602
  • 16
  • 26
1
NSString *s = @"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"%@", s); // => foobarbazfoo

Replace multiple characters in a string in Objective-C?

Community
  • 1
  • 1
Puvanarajan
  • 2,786
  • 6
  • 26
  • 37
0

Since the back slash character is an escape character you have to write two of them.

- (NSString *)sanitizeString:(NSString *)string {
    NSCharacterSet* illegalCharacters = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
    return [[string componentsSeparatedByCharactersInSet: illegalCharacters] componentsJoinedByString:@"n"];
}
Mikael
  • 3,572
  • 1
  • 30
  • 43
  • Try it, it does not change the string. – zaph Jun 24 '14 at 11:57
  • In this case it was the combination of \n that made it not work. Updated the code. – Mikael Jun 24 '14 at 12:04
  • If you try the string "bmcvn\nmsf" you will get the result "bmcvnnmsf". So that works. However, a special case (at least for my code), needs to be done if it contains "\r" – Mikael Jun 24 '14 at 12:19
  • OK, interesting answer and I missed it. The problem is that it does not address the basic misunderstanding about escaped characters. – zaph Jun 24 '14 at 12:31
  • Ok @Zaph. You obviously knows more about this than I do! :) I'll up vote your answer. – Mikael Jun 24 '14 at 12:33