2

I have the need to replace a set of characters with a single character. Ok No problem. There are a slew of ways to go about this. Currently, I am implementing the following:

-(NSString *)spank:(NSString *)string
{
    NSCharacterSet *buggers = [NSCharacterSet characterSetWithCharactersInString:BAD_CHARS];
    return [[string componentsSeparatedByCharactersInSet:buggers]
                   componentsJoinedByString:GOOD_CHAR];
}

My problem is discovering other sets of characters that are in need of being replaced within that same string.

e.g.

#define BAD_CHARS @"`^"
#define GOOD_CHAR @"|"
#define TEST @"Lor*em` ipsum dolor ^sit amet"
...
clean = [self spank:TEST];

console log: clean = "Lor*em| ipsum dolor |sit amet"
But it would be nice to replace * with a different character. Other cases could be added later.


To solve this, I could:

  1. Message spank repeatedly for the same string, granted I changed it's signature. (boring, repetitive, and inefficient)

    -(NSString *)spank:(NSString *)string bad:(NSString *)badset good:(NSString *)good;
    
  2. Use enumeration. (I have implemented this with success)

    -(NSString *)spank:(NSString *)string
    {
        static NSDictionary *blackbook;
        if (!blackbook) blackbook = [NSDictionary dictionaryWithObjectsAndKeys:
                                                  GOOD_CHAR_A, BAD_CHARS_A,
                                                  GOOD_CHAR_B, BAD_CHARS_B, nil];
    
         __block NSString *pure = string;
        [blackbook enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            NSCharacterSet *buggers = [NSCharacterSet characterSetWithCharactersInString:key];
            pure = [[pure componentsSeparatedByCharactersInSet:buggers]
                            componentsJoinedByString:obj];
        }];
    
         return pure;
    }
    
  3. Implement a coder from NSCoder. This is an area I haven't explored yet.

  4. Use php. I have the option to purge the string with php before I return it to the client. Perhaps it would be done easier there. Thoughts?

  5. Some other cleaner, easier way I haven't come up with yet but maybe you have.

Thanks in advance for any suggestions. I do have this working with solution #2. Just not as concise as I would like to see.

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
TheRed__
  • 164
  • 11
  • Wouldn't a regular expression (NSRegularExpression) be a good fit here? http://stackoverflow.com/questions/9276246/how-to-write-regular-expressions-in-objective-c-nsregularexpression – michaelok Sep 13 '13 at 16:49
  • Keep in mind I need to perform different actions based on the matched string. But this is worth looking at closer. – TheRed__ Sep 13 '13 at 17:04
  • 1
    I think you're looking for a Regex "Replace with callback" situation. Check this out: http://stackoverflow.com/questions/3957092/is-there-an-objective-c-regex-replace-with-callback-c-matchevaluator-equivalent – FrankieTheKneeMan Sep 13 '13 at 18:03

1 Answers1

2

for me the easier solution is this, but it maybe has not the best performance.

NSString *text = @"Lor*em` ipsum dolor ^sit amet";
NSDictionary *dictionary = @{@"*":@"",@"^":@"",@"`":@""};
for (NSString *key in dictionary)
{
    text = [text stringByReplacingOccurrencesOfString:key withString:dictionary[key]];
}
NSLog(@"TEXT : %@",text);

EDIT 1:

this also works for me, at least for that example

NSString *text = @"Lor*em` ipsum dolor ^sit amet";
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"*^`"];
text = [[text componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];
NSLog(@"TEXT : %@",text);
Camo
  • 1,778
  • 14
  • 12
  • Looks like someone finally understands what I'm trying to do. Now, my code simply expands on this idea providing any number of patterns and checks the string against each one. The NSRegularExpression process mentioned above will work equally well against one pattern. Not multiples. Correct me if I'm wrong. I think I'll stick with the dictionary method until I have more than a handful of patterns at which point an NSCoder will work better. – TheRed__ Sep 13 '13 at 23:42
  • Well, using the dictionary you may replace other wrong characters for any other, so I think is a good approach :) – Camo Sep 14 '13 at 03:05