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

I tried to print it, but these are just gaps because it is an escaped character. for example, the output should be as follows.

\correct 

Thanks in advance

+7
string ios xcode swift slash
source share
3 answers

For this, as well as in the future:

 \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. 
+36
source share

The backslash character \ acts as an escape character when used in a string. This means that you can use, for example, double quotes in a string, pre-expecting them with \ . The same applies to the backslash character, that is, println("\\") will only print \ .

+3
source share
 var s1: String = "I love my " let s2: String = "country" s1 += "\"\(s2)\"" print(s1) 

He will print I love my "country"

+1
source share

All Articles