简体   繁体   中英

IOS: stringByReplacingOccurrencesOfString don't work

filepath:/var/mobile/Applications/BA7AE6F8-C7EA-4601-A5F4-30E3C57FE948/Documents/photo2.jpg photonumber:/var/mobile/Applications/BA7AE6F8-C7EA-4601-A5F4-30E3C57FE948/Documents/2

index = 2;

NSString *nextSequentialFile =
            [filePath stringByReplacingOccurrencesOfString:photoNumber
                                                withString:[NSString stringWithFormat:@"%d", index+1]
                                                   options:NSBackwardsSearch
                                                     range:NSMakeRange(0, filePath.length)];

the result is ever nextsequenzial:/var/mobile/Applications/BA7AE6F8-C7EA-4601-A5F4-30E3C57FE948/Documents/photo2.jpg

why?

it must be /photo3.jpg

filepath doesn't contain photonumber , so there's nothing to replace. Note that photonumber ends with /Documents/2 while filepath contains /Documents/photo2.jpg .

photonumber needs to be changed to end in /Documents/photo2 and the replacement string needs to be changed to [NSString stringWithFormat:@"/photo%d", index+1] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM