Pergunta

I have file names in the following format

filename_ffffff.png
filename2_cccccc.png
...

How can I manipulate this string and replace its hex value with a value of my choosing.

say the new value is fffccc which would take filename_ffffff.png and turn it into filename_fffccc.png

-(NSString *)replace:(NSString *)input with:(NSString *)newHex{
//find in input a patern of 6 char hex followed by dot png
// remove it from the string
// add the newHex followed by png
}
Foi útil?

Solução

-(NSString *)replace:(NSString *)input with:(NSString *)newHex
{
    NSArray *seperated = [input componentsSeparatedByString:@"_"];
    return [NSString stringWithFormat:@"%@_%@.png", [seperated objectAtIndex:0], newHex];
}

Outras dicas

Do you mean

newstring = [oldstring stringByReplacingOccurrencesOfString:@"ffffff.png" withString:@"cccccc.png"];

It looks like you need to change the occurrence of the string value. Check this :

NSString *Str = [yourString stringByReplacingOccurencesOfString:@"ffffff.png" withString:@"fffccc"];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top