Encode NSString for XML/HTML

后端 未结 14 1210
误落风尘
误落风尘 2020-11-28 04:25

Is there a way to HTML encode a string (NSString) in Objective-C, something along the lines of Server.HtmlEncode in .NET?

14条回答
  •  有刺的猬
    2020-11-28 04:40

    Swift 4

    extension String {
        var xmlEscaped: String {
            return replacingOccurrences(of: "&", with: "&")
                .replacingOccurrences(of: "\"", with: """)
                .replacingOccurrences(of: "'", with: "'")
                .replacingOccurrences(of: ">", with: ">")
                .replacingOccurrences(of: "<", with: "<")
        }
    }
    

提交回复
热议问题