简体   繁体   English

iOS:stringByReplacingOccurrencesOfString不起作用

[英]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 文件路径:/var/mobile/Applications/BA7AE6F8-C7EA-4601-A5F4-30E3C57FE948/Documents/photo2.jpg照片编号:/ var / mobile / Applications / BA7AE6F8-C7EA-4601-A5F4-30E3C57FE948 / Documents / 2

index = 2; 索引= 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 结果是下一个序列:/var/mobile/Applications/BA7AE6F8-C7EA-4601-A5F4-30E3C57FE948/Documents/photo2.jpg

why? 为什么?

it must be /photo3.jpg 它必须是/photo3.jpg

filepath doesn't contain photonumber , so there's nothing to replace. filepath不包含photonumber ,因此没有可替换的内容。 Note that photonumber ends with /Documents/2 while filepath contains /Documents/photo2.jpg . 请注意, photonumber /Documents/2 filepath包含/Documents/photo2.jpg时, photonumber/Documents/2 /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] . 需要将photonumber更改为/Documents/photo2结尾,并将替换字符串更改为[NSString stringWithFormat:@"/photo%d", index+1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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