今天我们介绍iPhone开发的NSString与NSMutableString的常用方法。NSString 的 Class Reference 非常庞大,请参考官方文档:
NSString一直使用,但总没有好好整理它的用法。NSString与int,float等类型的转换见:
http://blog.csdn.net/htttw/article/details/7842955
直接上代码,注释很详细,不多说了:
-(void)testNSString
{
//初始化 NSString
NSString * str1 = [[NSString alloc] init];
str1 = @"This is NSString1";
NSLog(@"str1: %@", str1);
//NSString字符串的的长度
NSLog(@"str1的长度: %d", [str1 length]);
[str1 release];
//也可以用下面的方法初始化 NSString
NSString * str2 = [[NSString alloc] initWithString:@"This is NSString2"];
NSLog(@"str2: %@", str2);
[str2 release];
//也可以用char *创建 NSString字符串
char * chr = "This is NSString3";
NSString * str3 = [[NSString alloc] initWithCString:chr];
NSLog(@"str3: %@", str3);
[str3 release];
//创建格式化字符串
int a1 = 1, a2 = 2;
NSString * str4 = [[NSString alloc] initWithString:[NSString stringWithFormat:@"a1= %d, a2 = %d", a1, a2]];
NSLog(@"str4: %@", str4);
[str4 release];
//创建临时字符串(不用手动释放)
NSString * str6 = [NSString stringWithString:@"This is a temporary string2"];
NSLog(@"str6: %@", str6);
//注意: 在电脑的根目录下创建! 并不是在模拟器或iPhone真机上创建!
NSString * path = @"test.txt";
//NSString 与文件操作
//写文件
NSString * str7 = [[NSString alloc] initWithString:@"哈哈 This string is written to a file"];
NSString * str7_2 = [[NSString alloc] initWithCString:"哈哈 This string is written to a file" encoding:NSUTF8StringEncoding];
[str7 writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str7: %@", str7);
[str7 release];
[str7_2 writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str7_2: %@", str7_2);
[str7_2 release];
//读文件
NSString * str8 = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"str8: %@", str8);
[str8 release];
NSString * str8_2 = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"str8_2: %@", str8_2);
[str8_2 release];
//char *用strcmp比较
char * chr1 = "char1";
char * chr2 = "char2";
if(strcmp(chr1, chr2) == 0)
{
NSLog(@"chr1 等于 chr2");
}
else
{
NSLog(@"chr1 不等于 chr2");
}
//NSString *用isEqualToString比较(相等返回YES,否则返回NO)
NSString * str9 = @"String9";
NSString * str10 = @"String10";
NSString * str11 = @"String9";
NSLog(@"str9 isEqualToString:str10: %d", [str9 isEqualToString:str10]);
NSLog(@"str9 isEqualToString:str11: %d", [str9 isEqualToString:str11]);
//compare方法(comparer返回的三种值)
NSLog(@"NSOrderedSame = %d", NSOrderedSame);
NSLog(@"NSOrderedAscending = %d", NSOrderedAscending);
NSLog(@"NSOrderedDescending = %d", NSOrderedDescending);
NSString * str12 = @"String";
NSString * str13 = @"String";
//NSOrderedSame: 判断两者是否相同
NSLog(@"[str12 compare:str13]: %d", [str12 compare:str13]);
NSString * str14 = @"String";
NSString * str15 = @"string";
NSString * str16 = @"sting";
//逐字符按ASCII码比较
//简记为:
//如果NSOrderedSame,则前后相等
//如果NSOrderedAscending,则前小后大
//如果NSOrderedDescending,则前大后小
if([str14 compare:str15] == NSOrderedAscending)
{
NSLog(@"str14 小于 str15");
}
else
{
NSLog(@"str14 大于 str15");
}
if([str15 compare:str16] == NSOrderedDescending)
{
NSLog(@"str15 大于 str16");
}
else
{
NSLog(@"str15 小于 str16");
}
//不考虑大小写比较
if([str14 caseInsensitiveCompare:str15] == NSOrderedSame)
{
NSLog(@"不区分大小写: str14 等于 str15");
}
else
{
NSLog(@"不区分大小写: str14 不等于 str15");
}
//NSString拼接
NSString * str17 = @"str17";
NSString * str18 = @"str18";
//方法1(推荐)
NSString * str19 = [str17 stringByAppendingString:str18];
NSLog(str19, nil);
//方法2
NSString * str20 = [NSString stringWithFormat:@"%@%@", str17, str18];
NSLog(str20, nil);
//方法3
NSString * str21 = [@"" stringByAppendingFormat:@"%@%@", str17, str18];
NSLog(str21, nil);
//方法4
NSMutableString * ms = [[NSMutableString alloc] init];
[ms appendString:str17];
[ms appendString:str18];
NSLog(ms, nil);
[ms release];
//去除头尾空格
NSString * str22 = @" I have white space ";
NSString * str23 = [str22 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"str22: %@", str22);
NSLog(@"str23: %@", str23);
//换行
NSString * str24 = @"\n I am in the first line.\n I am in the second line.";
NSLog(str24, nil);
//换行写可以提高可读性
NSString * str25 = @"SELECT [CustomerID], [CustomerName] "
"FROM [Customer] "
"WHERE [CustomerID] = 1234";
NSString * str26 = @"SELECT [CustomerID], [CustomerName] \
FROM [Customer] \
WHERE [CustomerID] = 1234";
//str25 和 str26是一样的
NSLog(str25, nil);
NSLog(str26, nil);
//NSMutableString
NSMutableString * str27 = [NSMutableString stringWithString:@"hello world"];
NSLog(str27, nil);
//生成空字符串
NSMutableString * str28 = [NSMutableString string];
[str28 appendString:@"hello"];
[str28 appendString:@" world"];
NSLog(@"str28 = %@", str28);
//插入字符串
NSMutableString * str29 = [NSMutableString stringWithString:@"Mac X"];
//在第4个字符处(从0开始算)插入
[str29 insertString:@"OS " atIndex:4];
NSLog(@"str29 = %@", str29);
//删除字符串
NSMutableString * str30 = [NSMutableString stringWithString:@"Mac OS X"];
//deleteCharactersInRange:NSMakeRange(start, length): 从第start个字符开始,删除length个
[str30 deleteCharactersInRange:NSMakeRange(2, 3)];
NSLog(@"str30 = %@", str30);
//前缀和后缀
NSString * str31 = @"Mac OS";
NSString * str32 = @"Mac Pro";
//前缀
if([str31 hasPrefix:@"Ma"])
{
NSLog(@"Has prefix: Ma");
}
else
{
NSLog(@"Don't have prefix: Ma");
}
//后缀
if([str32 hasSuffix:@"c Pro"])
{
NSLog(@"Has suffix: c Pro");
}
else
{
NSLog(@"Don't have suffix: c Pro");
}
//字符串检索
NSString * str33 = @"Hello World! Welcome to Mac OS X";
//返回找到的第一个的 NSRange
NSRange range1 = [str33 rangeOfString:@"el"];
if(range1.location == NSNotFound)
{
NSLog(@" \'el\' not found in \'%@\'", str33);
}
else
{
NSLog(@" Found: location = %d, length = %d", range1.location, range1.length);
}
//不区分大小写搜索
NSRange range2 = [str33 rangeOfString:@"m" options:NSCaseInsensitiveSearch];
//从后往前搜索(如果找到,则返回的值仍是正方向数的序号)
NSRange range3 = [str33 rangeOfString:@"m" options:NSBackwardsSearch];
if(range2.location == NSNotFound)
{
NSLog(@"不区分大小写: \'m\' not found in \'%@\'", str33);
}
else
{
NSLog(@"不区分大小写: Found: location = %d, length = %d", range2.location, range2.length);
}
if(range3.location == NSNotFound)
{
NSLog(@"从后往前: \'m\' not found in \'%@\'", str33);
}
else
{
NSLog(@"从后往前: Found: location = %d, length = %d", range3.location, range3.length);
}
//截取字符串
//返回字符串开头至index位的字符串, 不包含索引位
NSString * str34 = [str33 substringToIndex:8];
//返回字符串第index位至末尾的字符串, 包含索引位
NSString * str35 = [str33 substringFromIndex:15];
//返回字符串中范围range内的字符串, 包含索引位. 即:
//substringWithRange:NSMakeRange(start, length): 返回从第start个字符开始的length个字符
NSString * str36 = [str33 substringWithRange:NSMakeRange(3, 15)];
NSLog(@"str34 = %@", str34);
NSLog(@"str35 = %@", str35);
NSLog(@"str36 = %@", str36);
}