标签:
1.字符串
* 字符串常用方法
* 字符串比较方法(经常用)
例如:
1).创建一个字符串对象方法:
NSstring * str1 = @"hello"; //1.直接赋值 //2.通过initWithString NSString * str = [[NSString alloc]initWithString:@"hello world!"]; <pre name="code" class="objc"> //3.initWithUTF8String把C的字符串转化为OC的字符串NSString * str_1 = [[NSString alloc]initWithUTF8String:"hello world"]; int a = 123;//用格式化字符串初始化 NSString * str_3 = [[NSString alloc]initWithFormat:@"a=%d%s%@", a,"abcd", @"efg"]; //可完成字符串的拼接以及完成C的字符串与OC的字符串的转化 NSString * str1 = [NSString stringWithString:@"hello world"]; NSString * str2 = [NSString stringWithUTF8String:"hello world"]; NSString * str3 = [NSString stringWithFormat:@"%s","hello world"];
NSString * str = @"hello"; const char *p = [str UTF8String];将OC字符串转化为C的字符串 NSString *str1 = @"123"; int a = [str intValue];//将数字串转化成整型数据 [str floatValue];//将数字串转化成float型 [str doubleValue];//将数字串转化成double型3).常用的字符串操作
<pre name="code" class="objc"> //求字符串长度 NSLog(@"%ld",[str1 length]);
//获取字符串中某个位置的字符 NSLog(@"%c", [str1 characterAtIndex:1]);
//比较两个字符串是否相等,相等返回YES(1),不相等返回NO(0) BOOL ret = [str1 isEqualTo:str2]; <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"> //比较两个字符串的大小,str1大于 返回1 相等返回0 小于返回-1 long ret1 = [str1 compare:str2]; <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"> //不区分大小写比较字符串大小 long ret2 = [str1 caseInsensitiveCompare:str2]; <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"> //将字符串中的所有小写字符转换成大写 不改变原来的字符串 NSString* ptr = [str2 uppercaseString]; <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"> //将字符串中的所有大写字符转换成小写 不改变原来的字符串 ptr = [str1 lowercaseString]; <span style="font-size:18px;"></span><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"> //将字符串中出现的第一个字母转换成大写,其余字母小写ptr = [str3 capitalizedString]; NSString* str4 = @"hello world";
<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"> //查找子串,找不到返回NSNotFound 找到返回location和lengthNSRangerange = [str4 rangeOfString:@"wr"]; if (range.location != NSNotFound) { NSLog(@"%ld %ld",range.location, range.length); } NSString* str5 = @"helloworld";
<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"> //字符串抽取 从下标0开始到4 不包括4 NSString* ptr1 = [str5 substringToIndex:4]; <pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"> //从下标5开始抽取到字符串结束 包括5 NSString* ptr2 = [str5 substringFromIndex:5]; //NSRange range = NSMakeRange(4,3); NSRange range = {4,3};//结构体初始化 <pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">//在range指定范围内抽取
<pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc"><pre name="code" class="objc">//在range指定范围内抽取//获取长度为3,从下标为4的字串 NSString* ptr3 = [str5 substringWithRange:range]; NSString* ptr4 = [str5 substringWithRange:NSMakeRange(4,3)]; NSString* str = @"www.1000phone.com";
【学习ios之路:Object-C】NSString和NSArray
标签:
原文地址:http://blog.csdn.net/zfx5130/article/details/42293281