Backspace (\\b) Equivalent in swift language

2019-09-14 02:03发布

问题:

What is the \b equivalent in swift? I have to split a string which is received from server with \b?

回答1:

From "Strings and Characters" in the Swift Reference:

Special Characters in String Literals

String literals can include the following special characters:

  • The escaped special characters \0 (null character), \\ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)
  • An arbitrary Unicode scalar, written as \u{n}, where n is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point

So Swift does not have a special character for the backspace character, like \b in the C language. You can use the Unicode special character \u{n}:

let string = "THIS\u{8}IS\u{8}A\u{8}TEST"

or create a string from the Unicode value:

let bs = String(UnicodeScalar(8))
let string = "THIS\(bs)IS\(bs)A\(bs)TEST"