objective-c 转换 & 到&在Objective-C中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1067652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:49:55  来源:igfitidea点击:

Converting & to & in Objective-C

iphoneobjective-cescapinghtml-entities

提问by nbojja

I have a URL string in the following format.

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

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

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

I want to replace &with &in the above URL. My result should be:

我想,以取代&&在上述网址。我的结果应该是:

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

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

Can someone post me the code to get this done?

有人可以向我发布代码来完成这项工作吗?

Thanks

谢谢

采纳答案by Chuck

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

回答by Michael Waterfall

Check out my NSString category for HTML. Here are the methods available:

查看我的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;

回答by Louis Gerbarg

There is no built-in function for this in the iPhone SDK. You should file a bugthat you want the functionality. In the normal Mac OS X SDK you can either load the fragment into an NSAttributedStringas HTML and ask it to hand back a plain string, or use CFXMLCreateStringByUnescapingEntities().

iPhone SDK 中没有为此提供内置函数。您应该提交一个想要该功能的错误。在普通的 Mac OS X SDK 中,您可以将片段作为 HTML加载到NSAttributedString中并要求它返回一个纯字符串,或者使用CFXMLCreateStringByUnescapingEntities()

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

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

回答by Richard Long

For iOS the following code should work for numeric codes. It should be relatively easy to extend to the likes of &amp;...

对于 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;

}

Some unit-test code ...

一些单元测试代码...

-(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 );

}