Objective-c的Foundation中常用类2——NSString和NSMutableString
来源:互联网 发布:java 引用其他文件方法 编辑:程序博客网 时间:2024/05/21 10:59
继续NSString,由于用的多,所以应该重点掌握
1、求字符串长度
NSString *myName = @"AOBAMA"; int length = (int)[myName length];//发现返回值类型是NSUInteger,不强转有警告,自己也不知道强转是不是常用的方法,望看到的网友能告知一下 NSLog(@"字符串的长度是:%d",length); if ([myName length] > 5) { NSLog(@"AAAAA");//打印AAAAA }
2、字符串的相关转换
改变大小写
NSString *string = @"Where are You,Boy!"; //都大写 NSLog(@"%@",[string uppercaseString]);//打印WHERE ARE YOU,BOY! //都小写 NSLog(@"%@",[string lowercaseString]);//打印where are you,boy! //所有单词首字母大写,其它字母小写 NSLog(@"%@",[string capitalizedString]);//打印Where Are You,Boy!
将字符串转化成基本数据类型
NSString *aboutFloat = @"3.14"; NSString *aboutBool = @"NO"; NSLog(@"%f",[aboutFloat floatValue]);//打印3.140000 NSLog(@"%d",[aboutBool boolValue]);//打印0 //把float型转化为整型 NSLog(@"%d",[aboutFloat intValue]);//打印3
把两个字符串合并成一个字符串
//方法一 NSString *str1 = @"Hello"; NSString *str2 = @" World"; NSString *str3 = [[NSString alloc]initWithFormat:@"%@%@",str1,str2]; NSLog(@"str1与str2合并成str3的结果为:%@",str3);//打印结果:str1与str2合并成str3的结果为:Hello World //方法二 NSString *str4 = [str1 stringByAppendingString:str2]; NSLog(@"str1与str2合并成str4的结果为:%@",str4);//打印结果:str1与str2合并成str4的结果为:Hello World //连接多个字符串 NSString *str5 = [str1 stringByAppendingFormat:@"%@%@%@",str1,str2,str3]; NSLog(@"str5为:%@",str5);//打印结果:str5为:HelloHello WorldHello World
3、是否以一个字符串开头hasPrefix:,是否以一个字符串结尾hasSuffix
NSString *str6 = @"who are you"; BOOL isStart1 = [str6 hasPrefix:@"wh"]; BOOL isStart2 = [str6 hasPrefix:@"h"]; BOOL isEnd1 = [str6 hasSuffix:@"u"]; BOOL isEnd2 = [str6 hasSuffix:@"o"]; NSLog(@"结果:%d,,,%d,,,%d,,,%d",isStart1,isStart2,isEnd1,isEnd2);//结果:1,,,0,,,1,,,0
4、将字符串按规则截取成数组
NSString *str7 = @"you/have/a/baby"; NSArray *array = [str7 componentsSeparatedByString:@"/"]; NSLog(@"%@",array);//数组可以直接打印不用遍历 /*打印结果 ( you, have, a, baby ) */
5、字符串的截取
NSString *str8 = [str7 substringFromIndex:2];//从第二个位置开始街区包括2,当然以0开头 NSLog(@"%@",str8);// u/have/a/baby NSString *str9 = [str7 substringToIndex:5];//从开始到规定的位置,但不包括该位置 NSLog(@"%@",str9);// you/h //根据范围截取 NSRange range = NSMakeRange(2, 3);//先定义范围,也可以这样定义NSRange range = {2,3}; NSLog(@"location..%ld length...%ld",range.location,range.length);// location..2 length...3 range.location = 3;//包括2 range.length =4;//从2开始长度为3的子字符串 NSLog(@"location..%ld length...%ld",range.location,range.length);// location..3 length...4 NSString *str10 = [str7 substringWithRange:range]; NSLog(@"%@",str10);// /hav6、字符串查询
NSRange range2 = [str7 rangeOfString:@"have"]; NSRange range3 = [str7 rangeOfString:@"aaa"]; NSLog(@"location1...%ld",range2.location); NSLog(@"location2...%ld",range3.location);
打印结果:
location1...4
location2...9223372036854775807
我们可以根据要查询的字符串在源字符串中的位置来判断查找结果。
9223372036854775807既为NSIntegerMax的值
所以我们在判断的时候可以这样
if(range3 == NSIntegerMax)
{
NSLog(@"该字符串包含");
}
关于NSMutableString(可变字符串)
NSMutableString继承与NSString,所以NSString中的所有方法都适用于它
1、插入字符串
NSMutableString *str = [[NSMutableString alloc]initWithFormat:@"hello"]; [str insertString:@"wwwwwww" atIndex:3]; NSLog(@"%@",str);
打印结果:
helwwwwwwwlo
2、删除字符串
[str deleteCharactersInRange:NSMakeRange(1, 4)]; NSLog(@"%@",str);
打印结果:
hwwwwwlo
3、替换字符串
[str replaceCharactersInRange:NSMakeRange(1, 3) withString:@"world"]; NSLog(@"%@",str);
打印结果:
hworldwwlo
- Objective-c的Foundation中常用类2——NSString和NSMutableString
- Objective-C: Foundation——NSString、NSMutableString、NSNumber、NSValue、NSDate
- Objective—C 关于NSString 和 NSMutableString
- Objective-C中字符串(NSString和NSMutableString)常用的方法
- Objective-C中NSString和NSMutableString的基本用法
- Foundation框架—字符串NSString 和 NSMutableString
- Objective-C NSString和NSMutableString
- Objective-C基础——NSString & NSMutableString
- 【Foundation学习笔记】常用类--NSString和NSMutableString
- 【黑马程序员】【Foundation框架】常用类NSString和NSMutableString字符串
- OC中Foundation框架字符串NSString和NSMutableString的使用
- Objective-C中NSString与NSMutableString的一些简单应用
- Objective-C----NSString 、 NSMutableString
- NSString NSMutableString objective-c中字符串操作
- Objective C - NSString & NSMutableString 的各种使用
- Objective-C Foundation框架实践——NSMutableString(一)
- Objective-C基础—字符串对象NSString,NSMutableString
- OutMan——Foundation框架中的NSString类和NSMutableString类
- 警告: No configuration found for the specified action: '/myNameSpace/login.action' in names
- java用正则表达式判断 数值,整数
- alternativa3d 文件模板
- 2013年计划
- Java关闭所有IE窗口
- Objective-c的Foundation中常用类2——NSString和NSMutableString
- Windows Store apps开发[85]Windows 8 开发31日-第27日-倾斜仪
- BeautifulSoup中文乱码解决问题 python 爬虫 乱码
- POJ 1503 Integer Inquiry
- php5.3.6的php_intl.dll加载不上解决方法
- C++之模板
- 网络摄象机常用传输协议
- 数据结构---->图的应用(拓扑排序,关键路径)
- 【Boost】boost::string_algo详解7——join的应用