本帖最后由 董月峰 于 2014-4-13 15:35 编辑
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) { NSString *str = @"www-it-heima-com"; NSLog(@"原字符串:%@",str);
//创建一个新的可变字符串 NSMutableString *newStr = [str mutableCopy];
NSRange range = [newStr rangeOfString: @"-"];
while (range.location != NSNotFound) {
[newStr replaceCharactersInRange:range withString: @"."];
range = [newStr rangeOfString: @"-"]; }
NSRange range2 = [newStr rangeOfString: @"t.h"]; [newStr replaceCharactersInRange:range2 withString: @"th"];
NSLog(@"新字符串:%@",newStr);
}
楼主题目哪里找的,我重新做了一下,你看看
更贱的做法是
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) { NSString *str = @"www-it-heima-com"; NSLog(@"原字符串:%@",str);
//创建一个新的可变字符串 NSMutableString *newStr = [str mutableCopy];
NSRange range = [newStr rangeOfString: @"-it-heima-"]; [newStr replaceCharactersInRange:range withString: @".itheima."];
NSLog(@"新字符串:%@",newStr); }
|