Objective-C中的stringreplace
什么是replace字符的最佳方式是用于iPhone SDK的Objective-C中的string?
你可以使用这个方法
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement   …得到一个新的stringreplace一个子string(请参阅其他人的NSString文档) 
使用示例
 NSString *str = @"This is a string"; str = [str stringByReplacingOccurrencesOfString:@"string" withString:@"duck"]; 
  NSString对象是不可变的(它们不能被改变),但是有一个可变的子类NSMutableString ,它给你提供了几种replacestring中字符的方法。 这可能是你最好的select。 
如果你想多个stringreplace:
 NSString *s = @"foo/bar:baz.foo"; NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/:."]; s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""]; NSLog(@"%@", s); // => foobarbazfoo 
它也可以用stringreplacestringByReplacingCharactersInRange:withString:
 for (int i = 0; i < card.length - 4; i++) { if (![[card substringWithRange:NSMakeRange(i, 1)] isEqual:@" "]) { NSRange range = NSMakeRange(i, 1); card = [card stringByReplacingCharactersInRange:range withString:@"*"]; } } //out: **** **** **** 1234 
这个问题存在于iOS上的旧版本中。 最近,从右到左运作良好。 我做了什么,如下所示:
首先我查看iOS版本:
 if (![self compareCurVersionTo:4 minor:3 point:0]) 
比:
 // set RTL on the start on each line (except the first) myUITextView.text = [myUITextView.text stringByReplacingOccurrencesOfString:@"\n" withString:@"\u202B\n"]; 
 NSString *stringreplace=[yourString stringByReplacingOccurrencesOfString:@"search" withString:@"new_string"];