replacing escape character sequences in objective c - objective-c

I have a query that returns a string, as well as an escape character sequence. (ex. "USA\"")
I am using stringByReplacingOccurrencesOfString in this fashion:
[theCountry stringByReplacingOccurrencesOfString:#"\"" withString:#""];
But it is still leaving behind a set of quotes. and if I were to try the method again to remove them:
[theCountry stringByReplacingOccurrencesOfString:#""" withString:#""];
I would have an incomplete set of quotes...
I need to get rid of both the slash and the double quotes.
Any thoughts?

You need to escape the backslash:
NSString* foo = #"USA\\\"";
NSLog(#"String [%#]", foo);
foo = [foo stringByReplacingOccurrencesOfString:#"\\\"" withString:#""];
NSLog(#"String [%#]", foo);
Results in
2009-11-02 09:15:24.403 test[6098:903] String [USA\"]
2009-11-02 09:15:24.406 test[6098:903] String [USA]

Related

Replace special character or whitespace with -

I want to make every string look like this:
"this-is-string-one"
So main string could contain:
"This! is string, one"
So basicaly replace !?., whitespace and characters like that with "-".
Something like this is best handled by a regular expression:
NSString *someString = #"This! is string, one";
NSString *newString = [someString stringByReplacingOccurrencesOfString:#"[!?., ]+" withString:#"-" options: NSRegularExpressionSearch range:NSMakeRange(0, someString.length)];
NSLog(#"\"%#\" was converted to \"%#\"", someString, newString);
The output will be:
"This! is string, one" was converted to "This-is-string-one"
The regular expression [!?., ]+ means any sequence of one or more of the characters listed between the square brackets.
Update:
If you want to truncate any trailing hyphen then you can do this:
if ([newString hasSuffix:#"-"]) {
newString = [newString substringToIndex:newString.length - 1];
}

Encoding a comma with stringByAddingPercentEscapesUsingEncoding

Why doesn't the comma get encoded? I was expecting it to be %2C.
(lldb) po [#"," stringByAddingPercentEscapesUsingEncoding:4]
(id) $24 = 0x0a8fbfd0 ,
As noted by #DayS, because comma is a legal URL character. However, if you would like to have control over which characters are escaped, look at CFURLCreateStringByAddingPercentEscapes().
NSString *toencode = #",";
NSString *result =
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFTypeRef)toencode,
NULL,
CFSTR(","),
kCFStringEncodingUTF8));
NSLog(#"%#", result);
This method will only replace special characters which aren't valid in a URL. As the comma is a valid one, he'll stay like this.
Try with this string to check :
[#",éà /" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
To replace other characters like comma, you have to do it yourself...

(iOS) Error escaping a backslash in a string

I have a problem replacing a backslash in the string K90wuRcDX43cqDB7xkjReuzb\/nDaC0xc7Bqxi4Ft0T6EscKzEBXBJ6k8fFLl6j0S.
If I manually escape the backslash in the string I have no problem
NSString *mytoken = #"K90wuRcDX43cqDB7xkjReuzb\\/nDaC0xc7Bqxi4Ft0T6EscKzEBXBJ6k8fFLl6j0S";
NSLog(#"mytoken %#",mytoken);
mytoken K90wuRcDX43cqDB7xkjReuzb\/nDaC0xc7Bqxi4Ft0T6EscKzEBXBJ6k8fFLl6j0S
But if I try to do it programmatically then I can't escape the backslash (I tried with everything: CFString, stringByReplacingOccurrencesOfString, replaceOccurrencesOfString)
NSMutableString *mytokenOrig = [NSMutableString stringWithFormat:#"K90wuRcDX43cqDB7xkjReuzb\/nDaC0xc7Bqxi4Ft0T6EscKzEBXBJ6k8fFLl6j0S"];
[mytokenOrig replaceOccurrencesOfString:#"\\" withString:#"\\\\" options:NSCaseInsensitiveSearch range:(NSRange){0,[mytokenOrig length]}];
NSLog(#"mytokenOrig %#",mytokenOrig);
mytokenOrig K90wuRcDX43cqDB7xkjReuzb/
nDaC0xc7Bqxi4Ft0T6EscKzEBXBJ6k8fFLl6j0S
Note: I have to append this token to a string and calculate a hash, so it's so important to make it work.
That is because mytokenOrig doesn't contain a backslash, it already contains the escaped new-line.
[mytokenOrig replaceOccurrencesOfString:#"\n" withString:#"\\n" options:NSCaseInsensitiveSearch range:(NSRange){0,[mytokenOrig length]}];
However, you don't need to (re)escape a string. Escape characters are for you to be able to write special characters in code.

Replace double double quotes "" with single double quote " [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Remove double quotes from NSString
I have a string with double double quotes "" that I need to replace with single double quotes ".
I've tried the following but I still end up with double double quotes
NSString *str = #"This is a \"\"99\"\" string";
[str stringByReplacingOccurrencesOfString: #"\"\"" withString: #"\""];
Initially the string is: This is a ""99"" string
After the stringByReplacingOccurrencesOfString: This is a ""99"" string.
What am I doing wrong?
It looks like you are expecting str to contain your modified string. Instead, you should be looking at the return value of the stringByReplacingOccurrencesOfString:withString: function:
NSString *str = #"This is a \"\"99\"\" string";
NSString *result =[str stringByReplacingOccurrencesOfString: #"\"\"" withString: #"\""];
NSLog(#"before: %# after: %#", str, result);
NSString objects are immutable. If you want str to be mutable, look into NSMutableString: documentation link
The method you are calling doesn't change the original NSString. It returns a new NSString with the replacements you've asked for.
Save the new string and use that.

How do I remove '\' characters from my NSString

/images/content/booking_thumbs_uk/s_kl/50000/THB_999_H54007.jpg
changes to:
/images/content/booking_thumbs_uk/s_kl/00000/THB_999_H2470.jpg
NSString* original=#"\\/images\\/content\\/booking_thumbs_uk\\/s_kl\\/50000\\/THB_999_H54007.jpg";
NSString* removed=[original stringByReplacingOccurrencesOfString:#"\\" withString:#""];
NSLog(#"%#",removed); // shows /images/content/booking_thumbs_uk/s_kl/00000/THB_999_H2470.jpg
Be very careful, because inside the source code between "..." the backslash has a special meaning. In order to represent an honest backslash, you need to double it, like "\\".
You can use newString = [oldString stringByReplacingOccurrencesOfString:#"\\" withString:#""];