码迷,mamicode.com
首页 > 其他好文 > 详细

OC中字符串的提取与替换-四种不同方法实现

时间:2015-07-23 09:34:27      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:objective-c   ios开发   字符串   删除   替换   

/*
 1.将可变字符串 @"When I was young, I loved a girl in neighbor class."中,从 young提取到girl。替换 成@"a teacher, I rather to teach student "。
 */
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        /*方法一*/
        //NSMutableString * str = @"When I was young, I loved a girl in neighbor class.";
        /*
        NSMutableString * str1 = [[NSMutableString alloc]initWithString:@"When I was young, I loved a girl in neighbor class."];
        [str1 replaceCharactersInRange:NSMakeRange(11, 21) withString:@"a teacher, I rather to teach student"];
        NSLog(@"%@",str1);
        */
        
        /**方法二  先删除 再插入*/
        /*
        NSMutableString * str1 = [[NSMutableString alloc]initWithString:@"When I was young, I loved a girl in neighbor class."];
        [str1 deleteCharactersInRange:NSMakeRange(11,22)];
        //NSLog(@"%@",str1);
        [str1 insertString:@"a teacher, I rather to teach student" atIndex:11];
        NSLog(@"%@",str1);
        */
        
        /*方法三*/
        /*
        NSMutableString * str1 = [[NSMutableString alloc]initWithString:@"When I was young, I loved a girl in neighbor class."];
        NSString * str2 = [str1 stringByReplacingOccurrencesOfString:@"young, I loved a girl" withString:@"a teacher, I rather to teach student"];
        NSLog(@"%@",str2);
        */
        
        /*方法四*/
        NSMutableString * str1 = [[NSMutableString alloc]initWithString:@"When I was young, I loved a girl in neighbor class."];
         NSMutableString * str2=[str1 stringByReplacingOccurrencesOfString:@"young, I loved a girl" withString:@""];
        [str2 insertString:@"a teacher, I rather to teach student" atIndex:11];
        NSLog(@"%@",str2);

        
    }
    
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

OC中字符串的提取与替换-四种不同方法实现

标签:objective-c   ios开发   字符串   删除   替换   

原文地址:http://blog.csdn.net/u012701023/article/details/47016327

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