0

I am new in programming. I have string NSString *string = @"\U0420\U043e\U0437\U044b"; and after each slash('\') i need put another slash to get string like this @"\\U0420\\U043e\\U0437\\U044b"

I am new to programming and objective-c. please help.

2
  • It's not clear what you want as those strings are the same? Commented Feb 21, 2013 at 11:32
  • excuse me, I just corrected Commented Feb 21, 2013 at 11:34

1 Answer 1

3

My original answer was:

Use [NSString stringByReplacingOccurrencesOfString:withString:] (reference).

NSString *string = @"\U0420\U043e\U0437\U044b";
NSString *converted = [string stringByReplacingOccurrencesOfString:@"\\" 
                                                        withString:@"\\\\\\"];

However I now don't think that's right given the \ characters won't actually exist in string; instead the compiler will convert each of those sequences into a unicode character. You will need to encode string as this:

NSString *string = @"\\U0420\\U043e\\U0437\\U044b";

In order to use the above code. I cannot see any alternative to this.

Further Update: Often when I've come across questions like this there is a confusion between string literals and string data. In your question those \ characters won't appear as the compiler will have converted them into unicode characters (\Uxxx is a unicode escape sequence for a single character). However if you provided a string like that at runtime (say read from a text file) then those \ characters will exist and you can use the code above.

Sign up to request clarification or add additional context in comments.

3 Comments

but i can't do this because json gives me it as "\U0420\U043e\U0437\U044b" and i need to convert in to russian letters. i tried to convert it with [NSString stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding] but this method takes string as "\\U0420\\U043e\\U0437\\U044b" (with double slash)
@Asike How are you verifying what JSON gives you that string? If those slash characters actually exist in the string then you will need to do more than simply adding slashes. I also doubt whether stringWithCString will do what you want.
it seems that I need to do something with NSJSONSerialization, but anyway thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.