2009-07-01 10 views
22

Tengo una cadena de URL en el siguiente formato.Convertir & a & en Objective-C

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

quiero reemplazar & con & en la URL anterior. Mi resultado debe ser:

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

Puede alguien publicar el código para hacer esto?

Gracias

Respuesta

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

hice lo mismo ... pero ¿hay alguna manera incorporado a haz esto ... – nbojja

+2

@nbojja ¿Cuánto más construido adentro quieres? Si le preocupa, agregue un método que hace esto como una categoría en NSString y luego está integrado. – Abizern

+10

@Abizern: Muchos idiomas tienen métodos integrados para codificar y decodificar entidades HTML, Obj-C carece de esto y muchos otros cosas que los programadores dan por sentado desde 2002. Buscar y reemplazar es un sustituto pobre, porque tendrá que pasar bastante tiempo para saber que obtiene todas las entidades. –

8

no hay una función incorporada para esto en el iPhone SDK. Debe file a bug que desea la funcionalidad. En el SDK de Mac OS X normal, puede cargar el fragmento en un NSAttributedString como HTML y pedirle que le devuelva una cadena simple, o use 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

Esto no funciona con el recuento automático de referencias (ARC) {suspiro} – mpemburn

+0

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

+0

No se debe puentear a CFAllocatorRef, sino a CFStringRef. Eso también estaba mal en el listado del código original. – dgatwood

113

Compruebe hacia fuera mi NSString category for HTML. Aquí están los métodos disponibles:

// 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

Gracias por esto, Michael, ¡muy útil! (¡Tan útil como la respuesta a esta pregunta que fue aceptada es incorrecta!) –

+0

No hay problema;) ¡Me alegra que la haya encontrado útil! –

+0

Sí, muy útil, gracias Michael – Jack

4

Para iOS, el siguiente código debería funcionar para códigos numéricos. Debería ser relativamente fácil de extender a los gustos de &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; 

} 

Algunos de código de prueba de unidad ...

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

} 
Cuestiones relacionadas