更改NSString的部分

han*_*Dev 1 iphone objective-c nsstring ios

我有一个urlString: http://www.youtube.com/watch?v=5kdEhVtNFPo

我希望能够将其改为: http://www.youtube.com/v/5kdEhVtNFPo

我该怎么做呢?我不确定我是否应该使用实例方法substringWithRange:substringFromIndex:

我尝试了这个删除第一部分,只留下视频ID(它删除http://www.youtube.com/watch?v=)现在我只需要添加http://www.youtube.com/v/到字符串的开头.

NSString *newUrlString = [urlString substringFromIndex:31];
Run Code Online (Sandbox Code Playgroud)

And*_*Ley 9

NSString* newUrl = [oldUrl stringByReplacingOccurrencesOfString:@"watch?v=" withString:@"v/"];
Run Code Online (Sandbox Code Playgroud)

请注意,这只有在URL不包含字符串"watch?v ="的更多实例时才有效.