2010-06-29 17 views
6

Quiero simplemente reemplazar todas las ocasiones de "+" con un "" en blanco ... " Probé algunas muestras enumeradas aquí, también usé NSSMutableString, pero el programa se bloqueó ... cuál es el mejor forma de reemplazar un char de otro? graciasReemplazar un char en NSString

Respuesta

13

Si desea reemplazar con una cadena mutable (NSMutableString) en el lugar:

[theMutableString replaceOccurrencesOfString:@"+" 
            withString:@" " 
            options:0 
             range:NSMakeRange(0, [theMutableString length])] 

Si desea crear una nueva cadena inmutable (NSString):

NSString* newString = [theString stringByReplacingOccurrencesOfString:@"+" 
                  withString:@" "]; 
+0

gracias! ¡funciona bien! No sé ayer cuál fue el problema ... ¡ahora está bien! – ghiboz

+0

¿Tengo que liberar la cadena explícitamente más tarde? –

1
NSString *firstString = @"I'm a noob at Objective-C", *finalString; 

finalString = [[firstString stringByReplacingOccurrencesOfString:@"O" withString:@"0"] stringByReplacingOccurrencesOfString:@"o" withString:@"0"]; 

¡Obtengo el código de here!

Cuestiones relacionadas