Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with NSString's stringByReplacingOccurrencesOfString

60 views
Skip to first unread message

Sebastian Reitenbach

unread,
Nov 11, 2012, 8:07:14 AM11/11/12
to discuss...@gnu.org
Hi,

I try to replace "'" with "\'", and also "\\" with "\\\\" using NSString's stringByReplacingOccurrencesOfString.

When I for example do this:
escapedFileName = [@"this is a string blah" stringByReplacingOccurrencesOfString:@"blah" withString:@"blubb"];
NSLog(@"escapedFileName: %@", escapedFileName);

Then it prints out:
escapedFileName: this is a string blubb

So far, so good.

but when I then try to:
escapedFileName = [@"this is a string' blah" stringByReplacingOccurrencesOfString:@"'" withString:@"\'"];
then the NSLog prints out:
escapedFileName: this is a string' blah

also when I try:
escapedFileName = [@"this is a string' blah" stringByReplacingOccurrencesOfString:@"'" withString:@"XXX"];
its still printing:
escapedFileName: this is a string' blah

also I have a problem when I try to escape \ characters:
escapedFileName = [@"this is a string\ blah" stringByReplacingOccurrencesOfString:@"blah" withString:@"blubb"];
prints:
escapedFileName: this is a string blubb
which replaced blah with blubb, but the backslash is also gone.

therefore, this doesn't work either:
escapedFileName = [@"this is a string\ blah" stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
just strips the backslash, instead of replacing it. NSLog prints:
escapedFileName: this is a string blah

I'm now wondering what the heck I'm doing wrong here?
Any hit with a cluestick is appreciated.
Its gnustep-base 1.24.0 I am using.

cheers,
Sebastian



Tom Davie

unread,
Nov 11, 2012, 8:11:21 AM11/11/12
to Sebastian Reitenbach, discuss...@gnu.org

On 11 Nov 2012, at 13:07, "Sebastian Reitenbach" <seba...@l00-bugdead-prods.de> wrote:

> Hi,
>
> I try to replace "'" with "\'", and also "\\" with "\\\\" using NSString's stringByReplacingOccurrencesOfString.
>
> When I for example do this:
> escapedFileName = [@"this is a string blah" stringByReplacingOccurrencesOfString:@"blah" withString:@"blubb"];
> NSLog(@"escapedFileName: %@", escapedFileName);
>
> Then it prints out:
> escapedFileName: this is a string blubb
>
> So far, so good.
>
> but when I then try to:
> escapedFileName = [@"this is a string' blah" stringByReplacingOccurrencesOfString:@"'" withString:@"\'"];
> then the NSLog prints out:
> escapedFileName: this is a string' blah

You have missed a quote, @"\'" is the string containing only a single quote, so you're asking it to replace quotes with the same quotes.
You wanted @"\'" in the first instance, and @"\\\'" in the second.

Tom Davie

Sebastian Reitenbach

unread,
Nov 11, 2012, 8:29:19 AM11/11/12
to Tom Davie, discuss...@gnu.org
Doh!. That works, many thanks.
With all my trials, I found this working:
escapedFileName = [@"this is a string' blah" stringByReplacingOccurrencesOfString:@"'" withString:@"XXXX"];
just to make sure, I retested with @"'" and with @"\'" as the first sring, and I get to the same result.
My first mail was a bit wrong here with that test I mentioned. That the @"'" worked for the first string, I did not
assumed I need that many backslashes for the second ;)

thanks Tom,

Sebastian

>
> Tom Davie





0 new messages