IOS:stringByReplacingOccurrencesOfString不起作用

时间:2011-10-18 13:20:25

标签: ios xcode string

文件路径:/var/mobile/Applications/BA7AE6F8-C7EA-4601-A5F4-30E3C57FE948/Documents/photo2.jpg photonumber:在/ var /移动/应用/ BA7AE6F8-C7EA-4601-A5F4-30E3C57FE948 /文档/ 2

index = 2;

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

结果永远 nextsequenzial:/var/mobile/Applications/BA7AE6F8-C7EA-4601-A5F4-30E3C57FE948/Documents/photo2.jpg

为什么?

一定是/photo3.jpg

1 个答案:

答案 0 :(得分:1)

filepath不包含photonumber,因此无需替换。请注意,photonumber/Documents/2结尾,filepath包含/Documents/photo2.jpg

photonumber需要更改为/Documents/photo2,并且替换字符串需要更改为[NSString stringWithFormat:@"/photo%d", index+1]