转换&在Objective-C至&[复制]转换&在Objective-C至&[复制](Convert

2019-05-14 09:16发布

这个问题已经在这里有一个答案:

  • Objective C的HTML逃生/ UNESCAPE 14个回答

我有以下格式的URL字符串。

http://myserver.com/_layouts/feed.aspx?xsl=4&web=%2F&page=dda3fd10-c776-4d69-8c55-2f1c74b343e2&wp=476f174a-82df-4611-a3df-e13255d97533

我想更换&&在上述URL。 我的结果应该是:

http://myserver.com/_layouts/feed.aspx?xsl=4&web=%2F&page=dda3fd10-c776-4d69-8c55-2f1c74b343e2&wp=476f174a-82df-4611-a3df-e13255d97533

有人可以张贴我的代码来完成这件事?

谢谢

Answer 1:

[urlString stringByReplacingOccurrencesOfString:@"&" withString:@"&"];


Answer 2:

看看我的NSString类别HTML 。 以下是可用的方法:

// Strips HTML tags & comments, removes extra whitespace and decodes HTML character entities.
- (NSString *)stringByConvertingHTMLToPlainText;

// Decode all HTML entities using GTM.
- (NSString *)stringByDecodingHTMLEntities;

// Encode all HTML entities using GTM.
- (NSString *)stringByEncodingHTMLEntities;

// Minimal unicode encoding will only cover characters from table
// A.2.2 of http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters
// which is what you want for a unicode encoded webpage.
- (NSString *)stringByEncodingHTMLEntities:(BOOL)isUnicode;

// Replace newlines with <br /> tags.
- (NSString *)stringWithNewLinesAsBRs;

// Remove newlines and white space from string.
- (NSString *)stringByRemovingNewLinesAndWhitespace;


Answer 3:

有一个在iPhone SDK对此没有内置函数。 你应该提交一个bug ,你想要的功能。 在正常的Mac OS X SDK您可以将片段加载到NSAttributedString为HTML,并要求它交还纯字符串,或使用CFXMLCreateStringByUnescapingEntities() 。

@interface NSString (LGAdditions)
- (NSString *) stringByUnescapingEntities;
@end

@implementation NSString (LGAdditions)
- (NSString *) stringByUnescapingEntities {
  CFStringRef retvalCF = CFXMLCreateStringByUnescapingEntities(kCFAllocatorDefault, (CFStringRef)self, NULL);
  return [NSMakeCollectable(retvalCF) autorelease];
}
@end


Answer 4:

对于iOS下面的代码应该适用于数字代码。 它应该是比较容易延伸到的喜欢&amp; ...

-(NSString*)unescapeHtmlCodes:(NSString*)input { 

NSRange rangeOfHTMLEntity = [input rangeOfString:@"&#"];
if( NSNotFound == rangeOfHTMLEntity.location ) { 
    return input;
}


NSMutableString* answer = [[NSMutableString alloc] init];
[answer autorelease];

NSScanner* scanner = [NSScanner scannerWithString:input];
[scanner setCharactersToBeSkipped:nil]; // we want all white-space

while( ![scanner isAtEnd] ) { 

    NSString* fragment;
    [scanner scanUpToString:@"&#" intoString:&fragment];
    if( nil != fragment ) { // e.g. '&#38; B'
        [answer appendString:fragment];        
    }

    if( ![scanner isAtEnd] ) { // implicitly we scanned to the next '&#'

        int scanLocation = (int)[scanner scanLocation];
        [scanner setScanLocation:scanLocation+2]; // skip over '&#'

        int htmlCode;
        if( [scanner scanInt:&htmlCode] ) {
            char c = htmlCode;
            [answer appendFormat:@"%c", c];

            scanLocation = (int)[scanner scanLocation];
            [scanner setScanLocation:scanLocation+1]; // skip over ';'

        } else {
            // err ? 
        }
    }

}

return answer;

}

一些单元测试代码...

-(void)testUnescapeHtmlCodes {

NSString* expected = @"A & B";
NSString* actual = [self unescapeHtmlCodes:@"A &#38; B"];
STAssertTrue( [expected isEqualToString:actual], @"actual = %@", actual );

expected = @"& B";
actual = [self unescapeHtmlCodes:@"&#38; B"];    
STAssertTrue( [expected isEqualToString:actual], @"actual = %@", actual );

expected = @"A &";
actual = [self unescapeHtmlCodes:@"A &#38;"];
STAssertTrue( [expected isEqualToString:actual], @"actual = %@", actual );

}


文章来源: Converting & to & in Objective-C [duplicate]