Search code examples
iosswiftstringescapingslash

(Swift) how to print "\" character in a string?


I have tried to print it but it just by passes because it's an escaped character. e.g output should be as follows.

\correct

Solution

  • For that and also future reference:

    \0 – Null character (that is a zero after the slash)
    \\ – Backslash itself.  Since the backslash is used to escape other characters, it needs a special escape to actually print itself.
    \t  – Horizontal tab
    \n – Line Feed
    \r  – Carriage Return
    \”  – Double quote.  Since the quotes denote a String literal, this is necessary if you actually want to print one.
    \’  – Single Quote.  Similar reason to above.