在Objective-C中替换字符串中的多个字符? [英] Replace multiple characters in a string in Objective-C?
本文介绍了在Objective-C中替换字符串中的多个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP中,我可以这样做:
In PHP I can do this:
$new = str_replace(array('/', ':', '.'), '', $new);
...替换所有字符实例/ :.使用空白字符串(删除它们)
...to replace all instances of the characters / : . with a blank string (to remove them)
我可以在Objective-C中轻松完成吗?或者我必须自己动手?
Can I do this easily in Objective-C? Or do I have to roll my own?
目前我正在多次调用 stringByReplacingOccurrencesOfString
:
strNew = [strNew stringByReplacingOccurrencesOfString:@"/" withString:@""];
strNew = [strNew stringByReplacingOccurrencesOfString:@":" withString:@""];
strNew = [strNew stringByReplacingOccurrencesOfString:@"." withString:@""];
谢谢,
亚光
Thanks,
matt
推荐答案
执行此操作的效率稍低:
A somewhat inefficient way of doing this:
NSString *s = @"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"%@", s); // => foobarbazfoo
查看 NSScanner
和 - [NSString rangeOfCharacterFromSet:...]
如果你想更有效地做这件事。
Look at NSScanner
and -[NSString rangeOfCharacterFromSet: ...]
if you want to do this a bit more efficiently.
这篇关于在Objective-C中替换字符串中的多个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文