常用的可变字符串处理实例方法
1.– appendFormat:
NSMutableString 这里需要注意的是该方法的处理方式是在原字符串上直接进行修改
1.– appendFormat:
Adds a constructed string to the receiver.
添加一个字符串构造的接收器
- (void)appendFormat:(NSString *)format …
NSMutableString *str = [NSMutableString stringWithFormat:@"lanouhenan"];
//1.在原字符串的基础上拼接Frank appendFormat 对于字符串的修改都是在字符串基础上修改,所以修改完的字符串内容发生变化 所以不需要返回值
[str appendFormat:@"Frank"];
NSLog(@"%@",str);// lanouhenanFrank
[str appendFormat:@"Frank"];
NSLog(@"%@",str);// lanouhenanFrank
Adds to the end of the receiver the characters of a given string.
增加了接收器的端部在给定的字符串的字符
- (void)appendString:(NSString *)aString
NSMutableString *str = [NSMutableString stringWithFormat:@"lanouhenan”];
[str appendString:@"Frank”];
-
-
-
3.– deleteCharactersInRange:
-
Removes from the receiver the characters in a given range.
从接收机中删除给定范围内的字符
-
-
-
4.– insertString:atIndex:
-
Inserts into the receiver the characters of a given string at a given location.
插入到接收器给定的字符串中的字符在给定的位置。
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)anIndex
-
5.– replaceCharactersInRange:withString:
-
Replaces the characters from aRange with those in aString.
取代了aRange人物与那些在ASTRING。
NSMutableString *str = [NSMutableString stringWithFormat:@"lanouhenan”];
//5.替换字符串 在元字符串基础上将Frank替换成Duck
[str replaceCharactersInRange:NSMakeRange(10, 5) withString:@"Duck"];
NSLog(@"%@",str);//lanouhenanDuckFrank
-
6.– replaceOccurrencesOfString:withString:options:range:
-
Replaces all occurrences of a given string in a given range with another given string, returning the number of replacements.
替换在给定的范围内与其他给定的字符串给定的字符串的所有匹配,返回替换的数量。
- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)opts range:(NSRange)searchRange
NSMutableString *str1 = [NSMutableString stringWithFormat:@"lanouhenanjdsfhdfhjdfsdjfj”]; //可变的字符串处理函数
NSString * str13 = [str1 stringByReplacingCharactersInRange:NSMakeRange(10, 5) withString:@"Duck”];//不可变的字符串处理函数
NSLog(@"%@",str1);//lanouhenanjdsfhdfhjdfsdjfj
NSLog(@"%@",str13);//lanouhenanDuckdfhjdfsdjfj//这里可以结合函数的传值调用和传址调用,需要明白的NSString在处理字符串对象时是将字符串拷贝副本,针对副本进行操作 所以上面的返回值里有两中不同的结果.
-
7.– setString:
-
Replaces the characters of the receiver with those in a given string.
替换与一个给定的字符串中的接收器的特征。
- (void)setString:(NSString *)aString