zoukankan      html  css  js  c++  java
  • NSString 小技巧

    // 首字母大写

        NSString *string = @"liupengfei";

        NSLog(@" function: %s string: %@",__FUNCTION__,[string capitalizedString]);

     // 分割字符串

        NSString *string1 = @"I am iOS Engineer";

        NSArray *array = [string1 componentsSeparatedByString:@"iOS"];

        NSLog(@" function: %s array: %@",__FUNCTION__,array);

        NSString *str1 = [array objectAtIndex:0];

        NSString *str2 = [array objectAtIndex:1];

        NSLog(@" function: %s str1: %@ str2: %@", __FUNCTION__,str1, str2);

     

    // 追加字符串

        NSMutableString *muStr = [[NSMutableString alloc] initWithString:@"I Love "];

        [muStr appendString:@"iOS"];

        NSLog(@" function: %s muStr: %@", __FUNCTION__,muStr);

     

     // 插入字符串

        NSMutableString *muString = [[NSMutableString alloc] initWithString:@"I  iOS"];

        [muString insertString:@"Love" atIndex:2];

        NSLog(@" function: %s muString: %@", __FUNCTION__,muString);

     

    // 删除字符串

        NSMutableString *muString1 = [[NSMutableString alloc] initWithString:@"I Love iOS"];

        [muString1 deleteCharactersInRange:NSMakeRange(2, 4)];

        NSLog(@" function: %s muString1: %@", __FUNCTION__, muString1);

     

    // 判断是否包含前后缀

        NSMutableString *muString2 = [[NSMutableString alloc] initWithString:@"I Love iOS"];

        BOOL isHasI = [muString2 hasPrefix:@"I"];

        BOOL isHasA = [muString2 hasPrefix:@"A"];

        NSLog(@" function: %s isHasI: %d isHasA: %d", __FUNCTION__,isHasI,isHasA);

     

    // 替换字符串

        NSMutableString *muString3 = [[NSMutableString alloc] initWithString:@"I Love iOS"];

        NSString *replaceString = [muString3 stringByReplacingOccurrencesOfString:@"Love" withString:@"Hit"];

        NSLog(@" function: %s replaceString: %@", __FUNCTION__,replaceString);

     

     

    // 去除字符串首尾的空格和换行符

        NSString *string = @" I Love iOS ";

        NSString *text = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        NSLog(@" function: %s text:%@", __FUNCTION__,text);

    1
  • 相关阅读:
    python 引用和对象理解
    ABP .Net Core 部署到IIS 问题汇总
    Ionic2 cordova angular2 打包到Android apk环境搭建
    学习ABP ASP.NET Core with Angular 环境问题
    [AngularJS 2 实践 一]My First Angular App
    即时通信系统Openfire分析之一:Openfire与XMPP协议
    S3C6410启动过程分析
    使用Word发表博客园博文
    github学习笔记
    Mac环境下 配置Python数据分析环境
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/4897084.html
Copyright © 2011-2022 走看看