zoukankan      html  css  js  c++  java
  • OC中字符串的提取与替换-四种不同方法实现

    /*
     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;
    }
    

  • 相关阅读:
    使用手机重量加速器
    改变 Pivot 的 HeaderTemplate
    页面构造函数和 Load 事件的执行次数
    给 ListBox 的 DataTemplate 模板中的 元素设置动画
    在ItemsControl 中,添加头部下拉更新
    (转) Unix 时间戳 与 .NET 时间转换
    图片保存到本机(链接)
    IsHitTestVisible="False" 的功能
    回到顶部按钮
    ssh访问服务器端visdom
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6794994.html
Copyright © 2011-2022 走看看