0

Sorry for maybe a newbie question.

For various reasons I am stuck with a peculiar string that looks like this:

NSString *myString = @"A\\314\\212A\\314\\210O\\314\\210.jpg";

Can I in some ninja-way remove the double \\ and force NSString understand that the string is Uniencoded and should be read like this

NSString *myString = @"A\314\212A\314\210O\314\210.jpg"; // Displays ÅÄÖ as expected

I have tried different strategies tried to replace all slashes ("\"), but as soon as I add a ("\") NSString adds another one to escape the first one. And I get stuck here...

Is it possible to prevent NSString to escape my string?

UPDATE

I am aware this is a special case. Reading the output from a terminal program which reads files on the users drive. Via a NSTask I am capturing the output to into a NSString for parsing and splitting it into an array. It works great as long as there are no non-ascii characters. HFS+ is encoding non-ascii characters with slightly different Unicode called NFD.

When I am capturing the reponse, the ÅÄÖ are already encoded inside qoutes like this:

file.jpg
file2.jpg
"A\314\212A\314\210O\314\210.jpg"

When I create a NSString and with the captured reponse, it gets escaped by NSString a second time.

A\\314\\212A\\314\\210O\\314\\210.jpg

I am aware that this is not the optimal, but right now I have no control over what the terminal program is outputting. Usually when a NSString is created with this NFD encoding, Objectiv-C takes care of the encoding/decoding for you. But since I have a string with mixed and double escaped content, I have a hard way of creating it and make NSString to understand that the content is encoded with this encoding.

Basically I would like to to this:

decodedString = [output stringByReplacingOccurrencesOfString:@"\\\\"
                                                      withString:@"\\"];

But behind the scenes NSString is always escaping \ with another \ for you so I would like a way to create "raw" strings with out NSString interfering.

Have tried various ways to try enforing Unicode encoding on NSString but it all boils down to NSString is always capturing and escaping \.

Any tips och points appreciated!

jannej
  • 864
  • 14
  • 26
  • 1. You need to expand the question, it is not clear what you are asking. Provide more context. 2. What does "NSString adds another one to escape the first one" mean, please explain. – zaph May 27 '15 at 21:26
  • You could always just use the unicode characters: `NSString *myString = @"ÅÄÖ";`. It can also be expressed in composed UTF-8 as: `myString = @"\xc3\x85\xc3\x84\xc3\x96.jpg";` – zaph May 27 '15 at 21:49
  • 2
    Where did the value for `myString` come from? Is it hardcode? Received from the Internet? Something else? Provide more relevant code. – rmaddy May 27 '15 at 21:58
  • Sorry for the confusion, it was late and I was too into the problem I forgot to explain... ;-) The string is comming from a terminal program – jannej May 28 '15 at 05:45
  • How do you get the output of your tool into the NSString? If you're doing that right your string will contain exactly what the tool outputs. That means that a backslash stays a backslash which then might be escaped on output. Try looking at the length of your string. – Sven May 28 '15 at 06:45
  • Good point! I am using [NSString initWithData encoding] so it _should_ work. ;-) Like this: NSPipe *pipe = [NSPipe pipe]; [task setStandardOutput: pipe]; NSFileHandle *file = [pipe fileHandleForReading]; [task launch]; NSData *data = [file readDataToEndOfFile]; if(data.length == 0) { return nil; } NSString *output = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding]; – jannej May 28 '15 at 06:47

1 Answers1

0

I did not find any way around this other than go the other way around and change the output from the terminal program not to encode it this way.

jannej
  • 864
  • 14
  • 26