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读写操作excel数据
    python读写操作excel数据小应用
    操作系统相关知识
    网络编程课堂笔记
    UDP简单初识+socketserver 模块实现服务端并发
    链接循环+模拟远程执行命令+实现大文件上传
    循环通信
    luogu P2761 软件补丁问题
    luogu P4016 负载平衡问题
    P3381 【模板】最小费用最大流(spfa板子)
  • 原文地址:https://www.cnblogs.com/fantasy3588/p/4897084.html
Copyright © 2011-2022 走看看