How to Print Escape Sequence Characters in Swift

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

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.

escape characters in Swift

Newlines as \n work in Swift string literals too.

Check out the docs: Special Unicode Characters in String Literals

In Swift how to obtain the invisible escape characters in a string variable into another variable

The newline is the "raw character" contained in the string.

How exactly you formed the string (in this case from a string literal with an escape sequence in source code) is not retained (it is only available in the source code, but not preserved in the resulting program). It would look exactly the same if you read it from a file, a database, the concatenation of multiple literals, a multi-line literal, a numeric escape sequence, etc.

If you want to print newline as \n you have to convert it back (by doing text replacement) -- but again, you don't know if the string was really created from such a literal.

How to get JSON response without unicode escape sequence Swift

Your JSON string has some issues. When using \U it should send 4 bytes (8 hexa characters) but it is sending only 2 bytes (4 hexa characters). If you have control of what is being returned the easiest solution is to return \u instead of \U which is the correct unicode escape for 2 bytes otherwise you will have to do some string manipulation and apply a string transform before decoding your json:

let jsonData = Data(#"{"x_city":"\U041a\U0438\U0457\U0432"}"#.utf8)
let jsonString = String(data: jsonData, encoding: .utf8) ?? ""
let cleanedData = Data(
jsonString
.replacingOccurrences(of: #"\U"#, with: #"\u"#)
.applyingTransform(.init("Hex-Any"), reverse: false)!
.utf8
)
struct Response: Codable {
let xCity: String
}
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let response = try decoder.decode(Response.self, from: cleanedData)

print(response) // Response(xCity: "Київ")
} catch {
print(error)
}

Swift String including Special Characters

If you just want to replace the newlines with the literal characters \ and n then use:

let escapedText = someText.replacingOccurrences(of: "\n", with: "\\n")

Double quotes(escape character) inside String(format: ) in swift

Just do:

let timeString = String(format: "https://stackoverflow.com/character=\"inside\"").replacingOccurrences(of: "\"", with: "")

This will output as:

https://stackoverflow.com/character=inside

Replace the occurrences of " with nothing.

Happy coding!

swift can't decode string with escape sequence

You need 4 backslashes in the swift string to represent an actual backslash in model.str:

let json = """
{
"str": "\\\\",
}
"""
let jsonData = Data(json.utf8)

let decoder = JSONDecoder()

do {
let model = try decoder.decode(Model.self, from: jsonData)
print(model.str) // prints a single backslash
} catch {
print(error.localizedDescription)
}

A backslash in a JSON string needs to be escaped, so you need 2 backslashes in the JSON string, but to write this in a Swift string literal, you need to escape those two backslashes too. Hence the 4 backslashes.



Related Topics



Leave a reply



Submit