Encode NSString for XML/HTML

后端 未结 14 1396
误落风尘
误落风尘 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:55

    For URL encoding:

    NSString * encodedString = [originalString
          stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    

    See Apple's NSString documentation for more info.

    For HTML encoding:

    Check out CFXMLCreateStringByEscapingEntities, which is part of the Core Foundation XML library, but should still do the trick.

提交回复
热议问题