Search code examples
objective-cescapingnsstring

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).


Solution

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