码迷,mamicode.com
首页 > 移动开发 > 详细

ios 字符串的插入删除查找与替换,删除空格

时间:2015-04-27 02:02:05      阅读:707      评论:0      收藏:0      [点我收藏+]

标签:

NSString *str1 = @"This is a example.";  
NSMutableString *mstr = [[NSMutableString alloc] init]; //创建可变字符串  
NSRange substr; //子字符串的范围
mstr = [NSMutableString stringWithString:str1];  
//插入字符  
[mstr insertString:@"very easy " atIndex:10];  
//删除一些字符  
[mstr deleteCharactersInRange:NSMakeRange(10,5)];  
//查找并删除  
substr = [mstr rangeOfString:@"easy "];  
if (substr.location != NSNotFound) {
[mstr deleteCharactersInRange:substr]; 
  }  
//重新设置字符串  
[mstr setString:@"This is string A"];  
//替换字符串  
[mstr replaceCharactersInRange:NSMakeRange(15, 1) withString:@"BBB"];  
//查找第一个并替换  
NSString *search = @"This is";  
NSString *replace = @"An example of";  
substr = [mstr rangeOfString:search];  
if (substr.location != NSNotFound) {
[mstr replaceCharactersInRange:substr withString:replace]; 
  }  
//查找全部匹配的,并替换  
search = @"a";  
replace = @"X";  
substr = [mstr rangeOfString:search];  
while (substr.location != NSNotFound) {
[mstr replaceCharactersInRange:substr withString:replace];  
substr = [mstr rangeOfString:search];  
}  

//删除空格

[string stringByReplacingOccurrencesOfString:@" " withString:@""]

ios 字符串的插入删除查找与替换,删除空格

标签:

原文地址:http://www.cnblogs.com/huangzs/p/4458815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!