OC 自带方法 简绍 2 NSMutableString

4 篇文章 0 订阅
0 篇文章 0 订阅

1. NSMutableString   // 可以改变的string

+ (NSMutableString *)stringWithCapacity:(NSUInteger)capacity   //

NSMutableString * string = [NSMutableString stringWithFormat:@"蓝鸥"];


2.appendString   // 给可变字符添加字符串

- (void)appendString:(NSString *)aString  // Adds to the end of the receiver the characters of a given string.

例子:

       [string appendString:@"科技"];
        NSLog(@"%@", string);


3.deleteCharactersInRange  给一个范围 删除字符串

- (void)deleteCharactersInRange:(NSRange)aRange  // Removes from the receiver the characters in a given range.

例子:
        [string deleteCharactersInRange:NSMakeRange(1, 2)];
        NSLog(@"%@", string);


4.insertString  给一个起点 插入字符串

func insertString(_ aString: String,
          atIndex anIndex: Int)

//Inserts into the receiver the characters of a given string at a given location.

例子:

        [string insertString:@"鸥科" atIndex:1];
        NSLog(@"%@", string);


5.replaceCharactersInRange:   // 给一个范围用 给定的字符串替换

- (void)replaceCharactersInRange:(NSRange)aRange
                         withRTF:(NSData *)rtfData

//Replaces the characters in the given range with RTF text interpreted from the given RTF data.

        [string replaceCharactersInRange:NSMakeRange(1, 1) withString:@"鸟"];
        NSLog(@"%@", string);


6.setString  把内容重置

- (void)setString:(NSString *)aString // Replaces the characters of the receiver with those in a given string.

        [string setString:@""];
        NSLog(@" string = %@", string);


7. compare 比较两个字符串大小, 返回一个枚举型 NSComparisonResult

- (NSComparisonResult)compare:(id)otherCell  //Compares the string values of the receiver another cell, disregarding case.

        NSString * str1 = @"abc";
        NSString * str2 = @"aac";
        NSComparisonResult result = [str1 compare:str2];
        NSLog(@"%ld", result);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值