Swift 2.0 换行转义字符串(字符串编码)
Swift 2.0 Escaping string for new line (String Encoding)
我正在尝试转义新行的字符串,即 \n。
例如,假设字符串是:-
First Line Of String
second Line of String
Third Line of String
现在如果我使用字符串扩展并说
func escapeString() -> String{
newString = self.stringByRemovingPercentEncoding
return newString
}
这个扩展没有给我 newString as
First Line Of String\nSecond Line Of String\nThird Line Of String
我需要将以上字符串作为 jsonString 传递给 server.i.e。我必须对其进行字符串编码
stringByRemovingPercentEncoding
用于 percent encoding as used in URLs, as you might expect from the name. (If not from that, maybe from reading the docs,其相关部分甚至出现在 Xcode 代码完成中。)也就是说,它需要一个像 "some%20text%20with%20spaces" 这样的字符串,并且把它变成 "some text with spaces".
如果您想进行不同类型的字符替换,您需要自己进行。但这仍然可以是 one-liner:
extension String {
var withEscapedNewlines: String {
return self.stringByReplacingOccurrencesOfString("\n", withString: "\n")
}
}
注意 self.stringByReplacingOccurrencesOfString
的第一个参数是传递给 Swift 编译器的转义码,因此参数的实际值是换行符 (ASCII/UTF8 0x0A)。第二个参数转义了反斜杠(在传递给 Swift 编译器的文本中),因此参数的实际值是文本 \n
.
Swift 5
您可以使用 JSONEncoder
转义 \n、\、\t、\r、'、" 等字符,而不是在您的字符串中手动替换它们,例如:
extension String {
var escaped: String {
if let data = try? JSONEncoder().encode(self) {
let escaped = String(data: data, encoding: .utf8)!
// Remove leading and trailing quotes
let set = CharacterSet(charactersIn: "\"")
return escaped.trimmingCharacters(in: set)
}
return self
}
}
let str = "new line - \n, quote - \", url - https://google.com"
print("Original: \(str)")
print("Escaped: \(str.escaped)")
输出:
Original: new line -
, quote - ", url - https://google.com
Escaped: new line - \n, quote - \", url - https:\/\/google.com
我正在尝试转义新行的字符串,即 \n。 例如,假设字符串是:-
First Line Of String
second Line of String
Third Line of String
现在如果我使用字符串扩展并说
func escapeString() -> String{
newString = self.stringByRemovingPercentEncoding
return newString
}
这个扩展没有给我 newString as
First Line Of String\nSecond Line Of String\nThird Line Of String
我需要将以上字符串作为 jsonString 传递给 server.i.e。我必须对其进行字符串编码
stringByRemovingPercentEncoding
用于 percent encoding as used in URLs, as you might expect from the name. (If not from that, maybe from reading the docs,其相关部分甚至出现在 Xcode 代码完成中。)也就是说,它需要一个像 "some%20text%20with%20spaces" 这样的字符串,并且把它变成 "some text with spaces".
如果您想进行不同类型的字符替换,您需要自己进行。但这仍然可以是 one-liner:
extension String {
var withEscapedNewlines: String {
return self.stringByReplacingOccurrencesOfString("\n", withString: "\n")
}
}
注意 self.stringByReplacingOccurrencesOfString
的第一个参数是传递给 Swift 编译器的转义码,因此参数的实际值是换行符 (ASCII/UTF8 0x0A)。第二个参数转义了反斜杠(在传递给 Swift 编译器的文本中),因此参数的实际值是文本 \n
.
Swift 5
您可以使用 JSONEncoder
转义 \n、\、\t、\r、'、" 等字符,而不是在您的字符串中手动替换它们,例如:
extension String {
var escaped: String {
if let data = try? JSONEncoder().encode(self) {
let escaped = String(data: data, encoding: .utf8)!
// Remove leading and trailing quotes
let set = CharacterSet(charactersIn: "\"")
return escaped.trimmingCharacters(in: set)
}
return self
}
}
let str = "new line - \n, quote - \", url - https://google.com"
print("Original: \(str)")
print("Escaped: \(str.escaped)")
输出:
Original: new line -
, quote - ", url - https://google.com
Escaped: new line - \n, quote - \", url - https:\/\/google.com