- NSString *str1 = @"BeiJing";
- NSString *str2 = @"beijing";
-
- //全部转为大写
- NSLog(@"%@",[str1 uppercaseString]);
-
- //全部转为小写
- NSLog(@"%@",[str1 lowercaseString]);
-
- //首字母大写
- NSLog(@"%@",[str1 capitalizedString]);
-
- //比较两个字符串内容是否相同
- BOOL b =[str1 isEqualToString:str2];
-
- //两个字符串内容比较
- //NSOrderedAscending 右边 > 左边
- //NSOrderedSame 内容相同
- //NSOrderedDescending 左边 > 右边
- NSComparisonResult result = [str1 compare:str2];
- if (result == NSOrderedAscending) {
- NSLog(@"右边 > 左边");
- }else if(result == NSOrderedSame){
- NSLog(@"内容相同");
- }else if (result == NSOrderedDescending){
- NSLog(@"左边 > 右边");
- }
-
- //忽略大小写进行比较,返回值与compare一样
- result = [str1 caseInsensitiveCompare:str2];
- if (result == NSOrderedAscending) {
- NSLog(@"右边 > 左边");
- }else if(result == NSOrderedSame){
- NSLog(@"内容相同");
- }else if (result == NSOrderedDescending){
- NSLog(@"左边 > 右边");
- }
-
- //判断字符串是否以指定字符串开头
- [str1 hasPrefix:@"aaa"];
- //判断字符串是否以指定字符串结尾
- [str1 hasSuffix:@"aaa"];
-
- //判断字符串是否包含指定字符串,返回位置和长度
- NSRange range = [@"123456" rangeOfString:@"456"];
- NSLog(@"%@", NSStringFromRange(range));
-
- //反向搜索
- range = [@"123456456qweasasd456" rangeOfString:@"456" options:NSBackwardsSearch];
- NSLog(@"%@",NSStringFromRange(range));
-
- //指定范围进行搜索
- range = NSMakeRange(0, 9);
- range = [@"123456456qweasasd456" rangeOfString:@"456" options:NSBackwardsSearch range:range];
- NSLog(@"%@",NSStringFromRange(range));
复制代码 帮助大家快速节省代码
|
|