How to set a String to \

Viewed 26

I want to set a string to \ because I want to make a code which should take only a line from the text the user typed into an UITextView. And a for loop should detect \n.

But if I say "" Xcode says: "Unterminated string literal" Until I write """ but then the String only takes ". Please help me!

1 Answers

\ is used as an escape character. It is used to escape special characters that you would not usually be able to use within the string. For example you cannot use " in a string because it is the character used to start and end the string. If you type "\"" then the 2nd " is escaped and will be seen inside the string.

To use \ in a string, you have to escape it as well.

So the solution is to type \\.

Related