【问题标题】:Converting & to & in Objective-C [duplicate]在Objective-C中将&转换为& [重复]
【发布时间】:2010-11-07 05:57:13
【问题描述】:

我有一个如下格式的 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

有人可以把代码发给我吗?

谢谢

【问题讨论】:

    标签: iphone objective-c escaping html-entities


    【解决方案1】:

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

    【讨论】:

    • 谢谢你,迈克尔——非常方便! (就像这个被接受的问题的答案是错误的一样方便!)
    • 没问题 ;) 很高兴你发现它有用!
    • 是的,非常有用,谢谢迈克尔
    • 感谢您的帮助...干得好!
    • 谢谢!你刚刚为我节省了很多时间!!
    【解决方案2】:
    [urlString stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    

    【讨论】:

    • 我也是这样做的...但是有没有内置的方法可以做到这一点...
    • @nbojja 你还想要多少内置功能?如果您对此感到担忧,请在 NSString 上添加一个将其作为类别执行此操作的方法,然后将其内置。
    • @Abizern:许多语言都有内置的方法来编码和解码 HTML 实体,Obj-C 缺乏这个以及自 2002 年以来程序员认为理所当然的许多其他东西。搜索和替换是一个糟糕的替代品,因为您将不得不花费相当长的时间才能知道您获得了所有实体。
    • 精湛的回答谢谢:)
    【解决方案3】:

    iPhone SDK 中没有内置函数。你应该file a bug 你想要这个功能。在普通的 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
    

    【讨论】:

    • 这不适用于自动引用计数 (ARC) {sigh}
    • @mpemburn 你试过了吗:` CFStringRef retvalCF = CFXMLCreateStringByUnescapingEntities(kCFAllocatorDefault, (__bridge CFAllocatorRef)self, NULL);返回 (NSString *)CFBridgingRelease(retvalCF);`
    • 它不应该被桥接到CFAllocatorRef,而是CFStringRef。这在原始代码清单中也是错误的。
    【解决方案4】:

    对于 iOS,以下代码应适用于数字代码。它应该相对容易扩展到&amp;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 );
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-12
      • 2017-01-01
      • 2013-09-01
      • 1970-01-01
      • 2018-08-10
      • 2013-03-31
      • 1970-01-01
      • 2019-12-17
      相关资源
      最近更新 更多