OC中NSString字符串操作知识点归纳
NSString类常用函数大总结
#import
main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"helloworld";
//求字符串长度
NSInteger len = [str length];
NSLog(@"len = %li", len);
//获取指定位置的字符
unichar ch = [str characterAtIndex:10];
NSLog(@"ch = %C", ch);//打印OC中的字符用%C
//从指定位置提取子串到字符串结束
NSString *subStr1 = [str substringFromIndex:5];
NSLog(@"subStr1 = %@", subStr1);
//提取子串到指定位置(不包含传入位置的字符)
NSString *subStr2 = [str substringToIndex:5];
NSLog(@"subStr2 = %@", subStr2);
//提取指定范围的字符串
NSRange range = {5,5};
NSString *subStr3 = [str substringWithRange:range];
NSLog(@"subStr3 = %@", subStr3);
NSString *subStr4 = [str substringWithRange:NSMakeRange(10, 2)];
NSLog(@"subStr4 = %@", subStr4);
//字符串比较
//- (NSComparisonResult)compare:(NSString *)aString;
NSString *str1 = @"helloworld";
NSString *str2 = @"helloworld";
//大小写不敏感方式比较字符串对象
//返回值:NSOrderedAscending = -1: 递增顺序 str1 < str2
//NSOrderedSame: 相等 str1==str2
//NSOrderedDescending: 递减顺序 str1>str2
NSComparisonResult ret = [str1 caseInsensitiveCompare:str2];
NSLog(@"ret = %li", ret);
//判断两个字符串对象是否相等
BOOL ret1 = [str1 isEqualTo:str2];
NSLog(@"ret1 = %i", ret1);
//判断是否包含前缀子串
BOOL ret2 = [@"www.baidu.com" hasPrefix:@"www"];
NSLog(@"ret2 = %i", ret2);
//判断是否包含后缀子串
BOOL ret3 = [@"www.baidu.com" hasSuffix:@"com"];
NSLog(@"ret3 = %i", ret3);
//查找子串
NSString *str3 = @"www.hao123hao.com";
NSRange range1 = [str3 rangeOfString:@"hao"];//返回long类型范围最大值
if (range1.location==NSNotFound) {
NSLog(@"没有找到该子串 %lu", NSNotFound);
}
else
{
NSLog(@"location = %lu, length = %lu", range1.location, range1.length);
}
//倒序查找子串
NSRange range2 = [str3 rangeOfString:@"hao" options:NSBackwardsSearch];
if (range2.location == NSNotFound) {
NSLog(@"没有找到该子串");
}
else
{
NSLog(@"location = %lu, length = %lu", range2.location, range2.length);
}
//返回字符集合中的字符第一次在原字符串对象中出现的位置
NSRange range3 = [@"helloworld" rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"lo"]];
NSLog(@"location = %li length = %li", range3.location, range3.length);
//字符串追加
NSString *str4= @"hello";
NSString *str5 = [str4 stringByAppendingString:@"world"];
NSLog(@"str5 = %@", str5);
NSLog(@"str4 = %@", str4);
NSLog(@"str4 = %p str5 = %p", str4, str5);
//格式化追加字符串
NSString *str6 = [str4 stringByAppendingFormat:@"%s%d","world",567];
NSLog(@"str6 = %@", str6);
//把数字字符串转换成数字
float f = [@"3.56" floatValue];
int num= [@"8765" intValue];
NSInteger val = [@"098765" integerValue];
NSLog(@"f = %.2f num = %i val = %li", f, num, val);
//大小写转换
NSLog(@"%@", [@"qianfeng中国" uppercaseString]);//把小写字母转换成大写字母
NSLog(@"%@", [@"HELLOworld" lowercaseString]);//把大写字母转换成小写字母
NSLog(@"%@", [@"hello WORLD qianfeng" capitalizedString]);//每个单词的首字母大写,其余字母小写
//字符串替换
//用给定的字符串替换所有在原字符串中出现的目标字符串
NSString *str7 = [NSString stringWithUTF8String:"helloworldhelloqianhellofeng"];
NSString *str8 = [str7 stringByReplacingOccurrencesOfString:@"hello"withString:@"中国"];
NSLog(@"str8 = %@", str8);
//用给定的字符串对象替换指定范围内的字符
NSString *str9 = [str7 stringByReplacingCharactersInRange:NSMakeRange(5, 10)withString:@"千锋"];
NSLog(@"str9 = %@", str9);
//把OC字符串对象转换成C语言字符串
NSLog(@"%s", [@"helloworld" UTF8String]);
//返回公共前缀子串
NSString *str10 = @"helloworldqianfeng";
NSString *str11 = @"hellofengqian";
NSString *str12 = [str10 commonPrefixWithString:str11 options:NSLiteralSearch];
NSLog(@"str12=%@", str12);
}
return 0;
}
浙公网安备 33010602011771号