OC 自带方法 简绍 2 NSMutableString

来源:互联网 发布:公安网络布控 编辑:程序博客网 时间:2024/05/16 04:12

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
原创粉丝点击