0

I have a textview which can take any input and I need to parse the quotes and double quotes as ' and " respectively. So if I input a text in my text view like "Just testin'g quotes" the output should be "Just testin'g quotes" Below is my code for parsing and it works fine so far.

-(NSString *)parseQuotes:(NSMutableString *)str {
    [str replaceOccurrencesOfString:@"\"" withString:@""" options:0 range:NSMakeRange(0, [str length])];
    [str replaceOccurrencesOfString:@"'" withString:@"'" options:0 range:NSMakeRange(0, [str length])];

    return str;
}

So, now my problem is if I'm copy pasting a text like below

“Just testin’g quotes”

"Just testin'g quotes"

It gives me a output like

“Just testin’g quotes” 

"Just testin'g quotes"

Notice how the first one didn't parse as it has the quotes and double quotes in a different format. So for this I need to duplicate the same code with these new quotes like

    [str replaceOccurrencesOfString:@"“" withString:@""" options:0 range:NSMakeRange(0, [str length])];

    [str replaceOccurrencesOfString:@"”" withString:@""" options:0 range:NSMakeRange(0, [str length])];

    [str replaceOccurrencesOfString:@"’" withString:@"'" options:0 range:NSMakeRange(0, [str length])];

This works, but I need to know if this is the right way to proceed? Is there a possibility a different kind quote could appear and break this code? Is there a better alternative solution for this which will work for all types of quotes?

Francis F
  • 2,797
  • 3
  • 30
  • 75
  • https://stackoverflow.com/questions/803676/encode-nsstring-for-xml-html ? – Larme Jun 15 '17 at 13:57
  • You need to replace quotes with things like `"` probably because of some requirements. Different requirements will give you different set of quotes you need to replace (you are not going to replace all kinds of quotes in all possible UTF characters tables, right?). What are requirements for your case? – toma Jun 15 '17 at 14:04
  • @AndrewTomenko, basically user can type or paste any type of content in the textview. So, I need to handle for any type of quote they can paste. – Francis F Jun 15 '17 at 14:26
  • @Gamerlegend user can paste really wide range of character that may be called quotes. But from the point of development, generally there is no need to handle them all. For example if you going to store user input in xml and you need to escape quotes - you will only need to escape only one version of them, and it is `"` character. – toma Jun 15 '17 at 14:51

2 Answers2

0

While the set of potential replacements is subject to change, a nice way to handle this is with componentsSeparatedByCharactersInSet: Here's a good resource for the potential replacements.

static NSCharacterSet *characterSet = [NSCharacterSet
    characterSetWithCharactersInString:@"\"''ʺ˝ˮ˶״᳓“”‟″‶〃""];
NSString *newStr = [[str componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@"""]
danh
  • 55,236
  • 10
  • 89
  • 124
0

Start by defining a mapping:

NSMutableDictionary *mapping = [NSMutableDictionary dictionary];
mapping[@"\""] = @""";
mapping[@"”"] = @""";
...

for (NSString *key in mapping) {
    [str replaceOccurrencesOfString:key withString:mapping[key] options:0 range:NSMakeRange(0, [str length])];
}

That's an easily scalable solution.

Instead of using replaceOccurrencesOfString:withString: you might want to just iterate the characters and check every character separately, to improve the performance.

Sulthan
  • 118,286
  • 20
  • 194
  • 245
  • Immutable strings would be faster, too, but it seems the application in this case is dealing with user input, small enough that anything reasonable will run in real time. – danh Jun 15 '17 at 14:27
  • @danh Nowadays even long texts can be done fast. It all depends on the number or replacements. – Sulthan Jun 15 '17 at 14:28
  • It also depends if you want to replace character sequences, e.g. 3 literal dots to `&hellip`. – Sulthan Jun 15 '17 at 14:33