Foundation - NSMutableString
/*
* 字符串的替换和插入
*/
void stringReplace()
{
NSMutableString *str = [NSMutableString stringWithFormat:@"/users/mj/abc.txt"];
NSRange range = [str rangeOfString:@"abc"];
[str replaceCharactersInRange:range withString:@"cde"]; //将abc替换为cde
NSUInteger location = [str rangeOfString:@"/cde"].location;
[str insertString:@"/jj" atIndex:location]; //在cde前面加入“/jj”上层目录
NSLog(@"str=%@", str); //str=/users/mj/jj/cde.txt
}
/*
*字符串删除
*/
void stringDelete()
{
NSMutableString *str = [NSMutableString stringWithFormat:@"/users/mj/abc/mj"];
// 默认返回是第一个mj的范围
//NSRange range = [str rangeOfString:@"mj"];
// NSBackwardsSearch 从字符串尾部开始搜索内容
NSRange range = [str rangeOfString:@"mj" options:NSBackwardsSearch];
// [str rangeOfString:<#(NSString *)#> options:<#(NSStringCompareOptions)#> range:<#(NSRange)#>];
// 删除末尾的mj
[str deleteCharactersInRange:range];
NSLog(@"str=%@", str); //str=/users/mj/abc/
}
/*
*可变字符串的拼接
*/
void stringApped()
{
NSString *qq = @"9934343";
NSString *pwd = @"dfsfds432432";
//NSString *url = [NSString stringWithFormat:@"http://im.qq.com?qq=%@&pwd=%@", qq, pwd];
NSMutableString *url = [NSMutableString stringWithFormat:@"http://im.qq.com?"];
[url appendFormat:@"qq=%@", qq];
[url appendString:@"&"];
[url appendFormat:@"pwd=%@", pwd];
NSLog(@"%@", url); //http://im.qq.com?qq=9934343&pwd=dfsfds432432
}
/*
*可变字符串的简单实用
*/
void stringUse()
{
NSMutableString *str = [NSMutableString stringWithFormat:@"Jack"];
// 拼接到str的后面
[str appendString:@" Mike"];
[str appendFormat:@" age is %d and height is %f", 20, 1.55f];
NSLog(@"str=%@", str); //str=Jack Mike age is 20 and height is 1.550000
}