Pat_Morita
Pat_Morita

Reputation: 3535

Escaping custom character in objective-c

I am working on macOS, not iOS, XCode 11.

My app allows in a specific location to enter text. This text can be anything. Once done it exports a csv which will be passed to an external process i cannot influence.

The issue: the external process uses semicolon ";" as a separator (csv is separated differently). If the user writes semicolon the external process will fail. If I manually add an escaping backslash before each semicolon to the csv and then pass it to the external app it works.

What I need: having each semicolon escaped with ONE backslash in the final csv

What I tried

  1. Escaping the whole text with quotation marks - fail
  2. Escaping semicolons in objective-c before writing csv by trying stringByReplacingOccurrencesOfString (look for @";" replace with @"\;" - compiler throws a warning that escape character is unknown - fail

Appreciate any help

UPDATE: I also tried to set a double backslash like @Corbell mentioned but this leads in a double backslash in the exported CSV -> fail I also tried to set a single backslash by using its unicode character:

[NSString stringWithFormat:@"%C;",0x5C]; --> "\\;"

Also failed and produces two backslashes in the final CSV (where i need ONE only).

Upvotes: 2

Views: 213

Answers (2)

Pat_Morita
Pat_Morita

Reputation: 3535

Solved. It was the CSV Parser that added additional escaping characters. Once solved that it worked like a charm.

Upvotes: 0

Corbell
Corbell

Reputation: 1393

In your stringByReplacingOccurencesOfString call, second parameter, try escaping your backslash with a backslash to make it a literal character to insert, i.e. @"\\;" - otherwise the compiler thinks you're trying to specify @"\;" as an escape sequence (backslash-semicolon) which is invalid.

Upvotes: 1

Related Questions