Encode NSString for XML/HTML

后端 未结 14 1350
误落风尘
误落风尘 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条回答
  •  -上瘾入骨i
    2020-11-28 04:45

    I took Mike's work and turn it into a category for NSMutableString and NSString

    Make a Category for NSMutableString with:

    - (NSMutableString *)xmlSimpleUnescape
    {
        [self replaceOccurrencesOfString:@"&"  withString:@"&"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@""" withString:@"\"" options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@"'" withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@"'"  withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@"’" withString:@"'"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@"–" withString:@"-"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@">"   withString:@">"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@"<"   withString:@"<"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    
        return self;
    }
    
    - (NSMutableString *)xmlSimpleEscape
    {
        [self replaceOccurrencesOfString:@"&"  withString:@"&"  options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@"\"" withString:@""" options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@"'"  withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@">"  withString:@">"   options:NSLiteralSearch range:NSMakeRange(0, [self length])];
        [self replaceOccurrencesOfString:@"<"  withString:@"<"   options:NSLiteralSearch range:NSMakeRange(0, [self length])];
    
        return self;
    }
    

    Make a Category for NSString with:

    - (NSString *)xmlSimpleUnescapeString
    {
        NSMutableString *unescapeStr = [NSMutableString stringWithString:self];
    
        return [unescapeStr xmlSimpleUnescape];
    }
    
    
    - (NSString *)xmlSimpleEscapeString
    {
        NSMutableString *escapeStr = [NSMutableString stringWithString:self];
    
        return [escapeStr xmlSimpleEscape];
    }
    

    * A Swift 2.0 Version *

    The Objective-C version is a little more efficient as it does mutable operations on the string. However, this is a swift way to do simple escaping:

    extension String
    {
        typealias SimpleToFromRepalceList = [(fromSubString:String,toSubString:String)]
    
        // See http://stackoverflow.com/questions/24200888/any-way-to-replace-characters-on-swift-string
        //
        func simpleReplace( mapList:SimpleToFromRepalceList ) -> String
        {
            var string = self
    
            for (fromStr, toStr) in mapList {
                let separatedList = string.componentsSeparatedByString(fromStr)
                if separatedList.count > 1 {
                    string = separatedList.joinWithSeparator(toStr)
                }
            }
    
            return string
        }
    
        func xmlSimpleUnescape() -> String
        {
            let mapList : SimpleToFromRepalceList = [
                ("&",  "&"),
                (""", "\""),
                ("'", "'"),
                ("'",  "'"),
                ("’", "'"),
                ("–", "-"),
                (">",   ">"),
                ("<",   "<")]
    
            return self.simpleReplace(mapList)
        }
    
        func xmlSimpleEscape() -> String
        {
            let mapList : SimpleToFromRepalceList = [
                ("&",  "&"),
                ("\"", """),
                ("'",  "'"),
                (">",  ">"),
                ("<",  "<")]
    
            return self.simpleReplace(mapList)
        }
    }
    

    I could have used the NSString bridging capabilities to write something very similar to the NSString version, but I decided to do it more swifty.

提交回复
热议问题