Replace double quote(") with escape double quote(\") is not working in iOS 11

Viewed 1434

I am trying to replace double quote (") with escape double quote(\"), but it is unable to replace in iOS 11. Same works fine till iOS 10. I am using Xcode 9.1.

Example:

Input from search bar : "Course"

NSString *modifedSearchString = [searchBar.text stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""\""];

Expected Output : \"Course\" (Working this till iOS 10)

O/p in iOS 11 : "Course"

Anyone facing same issue?

Thanks in advance!!

2 Answers

iOS 11 has added "Smart Punctuation" to the keyboard settings (see screenshot).

This means when typing "Course" it will convert it to “Course”. The '"' are replaced by a '“' and '”' string. These are different quotes (smart quotes) compared to the default ones.

So one option might be to also replace '“' and '”' by '\"'.

Screenshot

@KishanLal Try this...

NSString *str = [NSString stringWithFormat:@"\"Course\""];
    NSLog(@"str = %@", str); //str = "Course"
    NSString *modifedSearchString = [str stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""\""];
    NSLog(@"output = %@", modifedSearchString); //output = \"Course\"
Related