2009-07-01 17 views
22

Ho una stringa URL nel seguente formato.Conversione da & a & in Objective-C

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

voglio sostituire & con & nell'URL sopra. Il mio risultato dovrebbe essere:

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

Qualcuno mi può inviare il codice per ottenere questo fatto?

Grazie

risposta

14
[urlString stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; 
+0

ho fatto lo stesso ... ma c'è un modo integrato per fai questo ... – nbojja

+2

@nbojja Quanto più integrato vuoi? Se sei preoccupato, aggiungi un metodo che faccia questo come categoria su NSString e poi sia integrato. – Abizern

+10

@Abizern: molte lingue hanno metodi incorporati per codificare e decodificare le entità HTML, Obj-C non ha questo e molti altri cose che i programmatori danno per scontato dal 2002. La ricerca e la sostituzione sono un cattivo sostituto, perché dovrai dedicare un po 'di tempo a sapere che ottieni tutte le entità. –

8

non esiste alcuna funzione incorporata per questo in iPhone SDK. Si dovrebbe file a bug che si desidera la funzionalità. Nel normale Mac OS X SDK è possibile caricare il frammento in un file NSAttributedString come HTML e chiedergli di restituire una stringa semplice o utilizzare CFXMLCreateStringByUnescapingEntities().

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

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

Questo non funziona con Automatic Reference Counting (ARC) {sigh} – mpemburn

+0

@mpemburn hai provato: 'CFStringRef retvalCF = CFXMLCreateStringByUnescapingEntities (kCFAllocatorDefault, (__bridge CFAllocatorRef) self, NULL); return (NSString *) CFBridgingRelease (retvalCF); ' –

+0

Non deve essere collegato a CFAllocatorRef, ma piuttosto CFStringRef. Anche questo era sbagliato nel codice originale. – dgatwood

113

Controlla il mio NSString category for HTML. Ecco i metodi disponibili:

// 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; 
+0

Grazie per questo, Michael - molto utile! (Comodo come la risposta a questa domanda che è stata accettata è sbagliata!) –

+0

Nessun problema;) Felice di averlo trovato utile! –

+0

Sì, molto utile, grazie Michael – Jack

4

Per iOS il seguente codice dovrebbe funzionare per i codici numerici. Dovrebbe essere relativamente facile da estendere a personaggi del calibro di &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; 

} 

Alcuni codice unità-test ...

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

}