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

iOS 字符串中多个子符串的替换方法

时间:2016-03-09 14:16:03      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

//在字符串sting1 中将所有string2  替换成string3

-(NSString *)stringByReplacingCharactersInRangeString:(NSString *)string1 andString:(NSString *)string2 andorString:(NSString *)string3{
    
    NSUInteger cnt = 0, length = [string1 length];
    NSRange range = NSMakeRange(0, length);
    while(range.location != NSNotFound)
    {
        range = [string1 rangeOfString: string2 options:0 range:range];
        
        if(range.location != NSNotFound)
        {
            string1 = [string1 stringByReplacingCharactersInRange:range withString:string3];
            range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
            cnt++;
        }
    }
    return string1;
}

iOS 字符串中多个子符串的替换方法

标签:

原文地址:http://www.cnblogs.com/Lzhou/p/5257926.html

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