NSString encoding special characters like !@#$%^&

前端 未结 1 1008
情深已故
情深已故 2021-01-07 02:44

How can i encode my NSString so all the special character for example & becomes & and \' becomes &apos?

I am not sure if encoding is the right word fo

相关标签:
1条回答
  • 2021-01-07 03:20

    What you are talking about is called HTML Entities.

    There exists a category claiming to solve this: NSString+HTML.

    For URL Escaping (while we're at it) use this:

    @nterface NSString (Escaping)
    
    - (NSString *)percentEscapedString
    - (NSString *)percentUnescapedString
    
    @end
    
    @implementation NSString (Escaping)
    
    - (NSString *)percentEscapedString {
        return [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    
    - (NSString *)percentUnescapedString {
        return [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    
    @end
    

    Edit: As Dave pointed out weaknesses in my references solution, here are some related questions with lots of other solutions:

    • Objective C HTML escape/unescape
    • HTML character decoding in Objective-C / Cocoa Touch
    0 讨论(0)
提交回复
热议问题