0

I'm trying to remove an escape sequence (\p) from selected strings (for reuse in other places in my code). I want to create a separate object that holds that cleaned up string. Like this

(pseudo code)
 NSString *stringWithEscSequence = anEdgewoodSecretPlace.placeName;
 NSString *stringWithoutEscSequence = [removeTheEscSequenceP stringWithEscSequence];

 (where removeTheEscSequenceP is what I'm trying to figure out how to do)

....

I played with NSString formats and I found related questions at Position of a character NSString and how to replace one substring with another in Objective C? I can break out the substring that follows the escape sequence use ideas from the second posting. So I thought then I should try to locate the actual index of the escape sequence. But I can't figure out how to do that. Also I think there is probably a less messy, more straightforward way to do this, that I'm not thinking of.

I was playing around with the first posting ideas, though I knew that I was in a kind of primitive mode. But as soon as I attempt to replace "ABCD" with "\". I get an error 'Missing terminating """ = except if I put the ABCD string back in I'm not missing any quotes. I think it's because of the special nature ofo the \ character.

Even if I could figure out how to search for the "\" and then a "p" following that, I don't think that's really the best way to approach this

 NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCD"];


    NSRange range2 = [testCleanString rangeOfCharacterFromSet:charSet];

    if (range2.location == NSNotFound) {
        NSLog (@"Didn't find esc sequence");

    } else {
        NSLog (@"range.location is %lu",(unsigned long)range2.location );

    }
Community
  • 1
  • 1
LaurelS
  • 492
  • 2
  • 8
  • 22
  • 1
    could you explain why `stringByReplacingOccurrencesOfString:` does not work for you? – sergio Feb 23 '14 at 17:02
  • Can't explain it because that's probably what I need to learn how to use! I will look at that. Thank you. – LaurelS Feb 23 '14 at 18:03
  • NSString *newString = [testCleanString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; NSLog (@"newString is %@", newString); – LaurelS Feb 23 '14 at 18:11
  • The above worked and thanks to sergio, I know how to do something I didn't know before. Thanks. – LaurelS Feb 23 '14 at 18:12
  • I'm trying to figure out how to flag this as answered, since sergio's answer was a comment. I can't find a checkmark to check and could only vote up his answer. – LaurelS Feb 23 '14 at 18:14
  • glad it helped! I have added an answer below, so you can accept it. – sergio Feb 24 '14 at 08:48

1 Answers1

0

You could try with this:

NSString* stringWithoutEscSequence = [stringWithEscSequence stringByReplacingOccurrencesOfString:@"stringToRemove" withString:@"replacement"];

If you want to replace multiple characters you could do:

NSCharacterSet* toRemove = [NSCharacterSet characterSetWithCharactersInString:@"\n\p\t"];
NSString* stringWithoutEscSequence = [[stringWithEscSequence componentsSeparatedByCharactersInSet:toRemove] componentsJoinedByString: @""];
sergio
  • 68,819
  • 11
  • 102
  • 123