Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Escape Sequence characters in Swift?

Tags:

swift

Sorry if the title is not clear.

What I mean is this: If I have a variable, we'll call that a, with a value of "Hello\nWorld", it would be written as

var a = "Hello\nWorld

And if I were to print it, I'd get

Hello
World 

How could I print it as:

Hello\nWorld
like image 631
Will Avatar asked Apr 19 '17 01:04

Will


People also ask

How can I print Unicode characters in Swift?

Fortunately Swift can handle them very well, you can print unicode characters straight ahead or you can use the escape sequence by providing the hexa code of the unicode character. You can play around with emojis and look up unicode character codes for them on the Emojipedia website.

What are strings and characters in Swift?

Strings and Characters. A string is a series of characters, such as "hello, world" or "albatross". Swift strings are represented by the String type. The contents of a String can be accessed in various ways, including as a collection of Character values. Swift’s String and Character types provide a fast,...

What is \(variable) Escape format in Swift?

Long story short, this escape format ( \ (VARIABLE)) is called string interpolation and it's a really convenient & powerful tool for every beginner Swift programmer.

How to write an interpolated string in Swift?

) // Prints "Write an interpolated string in Swift using \ (multiplier)." To use string interpolation inside a string that uses extended delimiters, match the number of number signs after the backslash to the number of number signs at the beginning and end of the string.


Video Answer


4 Answers

I know this is a little old however I was looking for a solution to the same problem and I figured out something easy.

If you're wanting to print out a string that shows the escape characters like "\nThis Thing\nAlso this"

print(myString.debugDescription)

like image 104
Jordan Avatar answered Oct 20 '22 01:10

Jordan


Here's a more complete version of @Pedro Castilho's answer.

import Foundation

extension String {
    static let escapeSequences = [
        (original: "\0", escaped: "\\0"),
        (original: "\\", escaped: "\\\\"),
        (original: "\t", escaped: "\\t"),
        (original: "\n", escaped: "\\n"),
        (original: "\r", escaped: "\\r"),
        (original: "\"", escaped: "\\\""),
        (original: "\'", escaped: "\\'"),
    ]

    mutating func literalize() {
        self = self.literalized()
    }

    func literalized() -> String {
        return String.escapeSequences.reduce(self) { string, seq in
            string.replacingOccurrences(of: seq.original, with: seq.escaped)
        }
    }
}

let a = "Hello\0\\\t\n\r\"\'World"
print("Original: \(a)\r\n\r\n\r\n")
print("Literalized: \(a.literalized())")
like image 28
Alexander Avatar answered Oct 20 '22 01:10

Alexander


You can't, not without changing the string itself. The \n character sequence only exists in your code as a representation of a newline character, the compiler will change it into an actual newline.

In other words, the issue here is that the "raw" string is the string with the actual newline.

If you want it to appear as an actual \n, you'll need to escape the backslash. (Change it into \\n)

You could also use the following function to automate this:

func literalize(_ string: String) -> String {
    return string.replacingOccurrences(of: "\n", with: "\\n")
                 .replacingOccurrences(of: "\t", with: "\\t")
}

And so on. You can add more replacingOccurrences calls for every escape sequence you want to literalize.

like image 26
Pedro Castilho Avatar answered Oct 20 '22 02:10

Pedro Castilho


If "Hello\nWorld" is literally the string you're trying to print, then all you do is this:

var str = "Hello\\nWorld"
print(str)

I tested this in the Swift Playgrounds!

like image 42
Amiru Homushi Avatar answered Oct 20 '22 02:10

Amiru Homushi