NSString与NSMutableString的常用操作

字符串类 :  NSStringNSMutableString,即不可变字符串和可变字符串。

// NSString 常用算法
        //1.初始化方法
        NSString *str1 = [[NSString alloc]initWithFormat:@"啦啦啦"];
        NSString *str2 = [[NSString alloc]initWithString:str1];
        //2.便利构造器
        NSString *str3 = [NSString stringWithFormat:@"%@,生日快乐", str1];
        NSString * str4 = [NSString stringWithString:str3];
<span style="font-family: Menlo;">// 3. </span><span style="font-family: 'Heiti SC Light';">获取字符串长度</span>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">        <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"%lu"</span>, [str4 <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">length</span>]);</p><div>
</div><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"> <span style="white-space:pre">	</span><span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">NSString</span> *string = <span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">一大波马宏达</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">"</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(29, 148, 33);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">       <span style="white-space:pre">	</span></span>// 4.1 <span style="font-family: 'Heiti SC Light';">判断字符串是否包含某个前缀</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">        <span style="font-variant-ligatures: no-common-ligatures; color: #c32275">if</span> ([string <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">hasPrefix</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">一大波</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">"</span>]) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">            <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"%@</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">包含前缀</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">'</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">一大波</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">'"</span>, string);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">        }</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(29, 148, 33);"><span style="font-family: Menlo;"><span style="white-space:pre">	</span>// 4.2 </span>判断字符串是否包含某个后缀</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">        <span style="font-variant-ligatures: no-common-ligatures; color: #c32275">if</span> ([string <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">hasSuffix</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">马宏达</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">"</span>]) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">            <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"%@</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">包含后缀</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">'</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">马宏达</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">'"</span>, string);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">        }</p><div>
</div><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(29, 148, 33);"><span style="font-family: Menlo;">// 5. </span>判断子串在一个字符串中的范围</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #703daa"><span style="white-space:pre">	</span>NSRange</span> range = [string <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">rangeOfString</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(201, 27, 19);">大波</span><span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">"</span>];</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(29, 148, 33);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">        </span>//rangeOfString</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(29, 148, 33);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">        </span>//1.<span style="font-family: 'Heiti SC Light';">若没有找到</span>,<span style="font-family: 'Heiti SC Light';">会把</span>location<span style="font-family: 'Heiti SC Light';">的值赋成最大</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(201, 27, 19);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">        </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000">(</span>@"<span style="font-family: 'Apple Color Emoji';">
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值