Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple character set replacement algorithm
    primarykey
    data
    text
    <p>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:</p> <pre><code>-(NSString *)spank:(NSString *)string { NSCharacterSet *buggers = [NSCharacterSet characterSetWithCharactersInString:BAD_CHARS]; return [[string componentsSeparatedByCharactersInSet:buggers] componentsJoinedByString:GOOD_CHAR]; } </code></pre> <hr> <p><strong><code>My problem is discovering other sets of characters that are in need of being replaced within that same string.</code></strong></p> <p>e.g.</p> <pre><code>#define BAD_CHARS @"`^" #define GOOD_CHAR @"|" #define TEST @"Lor*em` ipsum dolor ^sit amet" ... clean = [self spank:TEST]; </code></pre> <p>console log: clean = "Lor*em| ipsum dolor |sit amet"<br>But it would be nice to replace * with a different character. Other cases could be added later.</p> <hr> <p>To solve this, I could:</p> <ol> <li><p>Message spank repeatedly for the same string, granted I changed it's signature. (boring, repetitive, and inefficient)</p> <pre><code>-(NSString *)spank:(NSString *)string bad:(NSString *)badset good:(NSString *)good; </code></pre></li> <li><p>Use enumeration. (I have implemented this with success)</p> <pre><code>-(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; } </code></pre></li> <li><p>Implement a coder from NSCoder. This is an area I haven't explored yet.</p></li> <li><p>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?</p></li> <li><p>Some other cleaner, easier way I haven't come up with yet but maybe you have.</p></li> </ol> <p>Thanks in advance for any suggestions. I do have this working with solution #2. Just not as concise as I would like to see.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload