//
// main.m
// OC5_NSMutableString操作
//
// Created by zhangxueming on 15/6/10.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import <Foundation/Foundation.h>
//NSMutableString 继承与NSString
//所有NSString类的方法NSMutableString 都可以使用
int main(
int argc,
const char *
argv[]) {
@autoreleasepool {
//创建指定容量大小的可变字符串对象
//+ (NSMutableString *)stringWithCapacity:(NSUInteger)capacity;
NSMutableString *mulStr1 = [[NSMutableString alloc] initWithCapacity:
20];
NSLog(@"mulStr1 = %@", mulStr1);
//替换指定范围内的字符
//- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;
NSMutableString *mulStr2 = [[NSMutableString alloc] initWithString:
@"hello world qianfeng"];
[mulStr2 replaceCharactersInRange:NSMakeRange(6,
5) withString:
@"welcome"];
NSLog(@"mulStr2 = %@", mulStr2);
//在指定位置增加字符串
NSMutableString *mulStr3 = [[NSMutableString alloc] initWithFormat:
@"千锋中国"];
[mulStr3 insertString:@"hello world" atIndex:
2];
NSLog(@"mulStr3 = %@", mulStr3);
//删除指定范围内的字符
NSMutableString *mulStr4 = [NSMutableString stringWithUTF8String:
"千锋hello world中国"];
[mulStr4 deleteCharactersInRange:NSMakeRange(2,
11)];
NSLog(@"mulStr4 = %@", mulStr4);
//追加字符串
NSMutableString *mulStr5 = [NSMutableString stringWithString:
@"helloworld"];
[mulStr5 appendString:@"qianfeng"];
NSLog(@"mulStr5 = %@", mulStr5);
//格式化追加字符串
NSMutableString *mulStr6 = [NSMutableString stringWithFormat:
@"%s%d",
"hello",
12345];
[mulStr6 appendFormat:@"%.2f%@",
3.14,
@"world"];
NSLog(@"mulStr6 = %@", mulStr6);
//修改字符串
NSMutableString *mulStr7 = [[NSMutableString alloc] initWithString:
@"hello world"];
[mulStr7 setString:@"qianfeng"];
NSLog(@"mulStr7 = %@", mulStr7);
}
return 0;
}
转载于:https://www.cnblogs.com/0515offer/p/4566859.html