Not reading special characters in string literals in swift

时间:2016-10-20 18:49:13

标签: ios swift

I have the following input txt file: "Hamlet \r William Shakespeare" "Romeo and Juliet \r William Shakespeare" "For the whom the bell tolls \r Earnest Hemingway"

I load it into an array and when I output it I get:

Hamlet \r William Shakespeare.

Why is it not reading the carriage return character?

Thanks

1 个答案:

答案 0 :(得分:0)

If you have \r in a file, it won't be read as the character \r (special return character), it will be read as 2 separate characters \ and r.

You can fix this by replacing the string "\r" with the special charater \r.

content = content.replacingOccurrences(of: "\\r", with: "\r")