查看文档(API)
OC的数据类型
NSInteger: 整型(32 = int, 64 = long)
CGFloat: 浮点型(32 = float, 64 = double)
如何学习系统的类
1.查看文档(API)
a. 类的概况(overview)
b. 类的继承关系(inherit)
c. 创建对象的方法(init, 自定义初始化方法, 便利构造器)
d. 找方法
NSString: 不可变字符串类(字符串一旦被创建, 就不能更改), 继承于NSObject
创建字符串对象
NSString *str1 = [NSString string];//空字符串 NSLog(@"%@", str1); NSString *str2 = @"iOS";//常量字符串 NSLog(@"%@", str2); NSLog(@"%p", str2);
NSString *str4 = [[NSString alloc]initWithFormat:@"Hello, %@", str2]; NSLog(@"%@", str4); NSInteger a = 10, b = 5; //创建一个字符串, "10 + 5 = 15" NSString *str5 = [[NSString alloc]initWithFormat:@"%ld + %ld = %ld", a, b, a + b ]; NSLog(@"%@", str5); //创建一个字符串, "10 + 5 = 105" NSString *str6 = [[NSString alloc]initWithFormat:@"%ld + %ld = %ld%ld", a, b, a, b]; NSLog(@"%@", str6); //创建一个字符串, "5 + 10 = 510" NSString *str7 = [NSString stringWithFormat:@"%ld + %ld = %ld%ld", b, a, b, a]; NSLog(@"%@", str7);
求字符串的长度
NSString *str8 = @"你问我爱你有多深?月亮代表我的心"; NSUInteger length = [str8 length]; NSLog(@"%lu", length);
获取子字符串"我爱你"
//NSRange range = {2, 3}; NSString *str9 = [str8 substringWithRange:range]; NSRange range1 = NSMakeRange(9, 2); NSString *str9 = [str8 substringWithRange:range1]; NSString *str9 = [str8 substringWithRange:NSMakeRange(9, 2)]; NSLog(@"%@", str9);
//"月亮代表我的心" NSString *str10 = [str8 substringFromIndex:9]; NSLog(@"%@", str10); //"你问我爱你有多深" NSString *str11 = [str8 substringToIndex:8]; NSLog(@"%@", str11); NSString *str12 = @"天再高又怎样"; NSString *str13 = @"踮起脚尖就更靠近阳光"; //"天再高又怎样踮起脚尖就更靠近阳光" NSString *str14 = [str12 stringByAppendingString:str13]; NSLog(@"%@", str14); //"天再高又怎样, 踮起脚尖就更靠近阳光" NSString *str15 = [str12 stringByAppendingFormat:@", %@", str13]; NSLog(@"%@", str15); NSString *str16 = [NSString stringWithFormat:@"%@, %@", str12, str13]; NSLog(@"%@", str16);
字符串的替换
NSString *str17 = @"微笑拥抱每一天, 做向日葵温暖的女子"; NSString *str18 = [str17 stringByReplacingOccurrencesOfString:@"女子" withString:@"女汉子"]; NSLog(@"%@", str18); NSString *str19 = @"baoshuang"; NSString *str20 = @"wenbin"; NSComparisonResult result = [str19 compare:str20]; switch (result) { case NSOrderedAscending://右边大 NSLog(@"%@ < %@", str19, str20); break; case NSOrderedSame://相等 NSLog(@"%@ = %@", str19, str20); break; case NSOrderedDescending://左边大 NSLog(@"%@ > %@", str19, str20); break; default: break; }
比较字符串是否相等
NSString *str21 = @"NBA"; NSString *str22 = @"CBA"; BOOL isSame = [str21 isEqualToString:str22]; if (isSame) { NSLog(@"相等"); } else { NSLog(@"不相等"); }
判断字符串是否以xxx开头
NSString *str23 = @"http://www.2048.com"; BOOL flag = [str23 hasPrefix:@"http://"]; if (flag) { NSLog(@"OK"); }
判断字符串是否以xxx结尾
NSString *str24 = @"guangen.png"; // NSString *str25 = @""; //判断是否以png结尾, 如果是, 把png改为avi flag = [str24 hasSuffix:@".png"]; if (flag) { NSString *str25 = [str24 stringByReplacingOccurrencesOfString:@"png" withString:@"avi"]; NSLog(@"%@", str25); } NSString *str26 = @"I loVe you Not who yoU are, but for who I am before you";
字符串改成小写
NSString *str27 = [str26 lowercaseString]; NSLog(@"%@", str27);
改成大写
NSString *str28 = [str26 uppercaseString]; NSLog(@"%@", str28);
首字母大写
NSString *str29 = [str26 capitalizedString]; NSLog(@"%@", str29);
字符串类型转换为基本类型(int, long, float, double)
NSString *str30 = @"123.456"; int number1 = [str30 intValue]; NSLog(@"%d", number1); NSInteger number2 = [str30 integerValue]; NSLog(@"%ld", number2); float number3 = [str30 floatValue]; NSLog(@"%f", number3); double number4 = [str30 doubleValue]; NSLog(@"%lf", number4);
转换回去
NSString *str31 = [NSString stringWithFormat:@"%d", number1]; NSLog(@"%@", str31);
NSMutableString, 可变字符串类(可以被编辑), 继承于NSString
创建可变字符串
NSMutableString *string = [[NSMutableString alloc]initWithCapacity:0]; NSLog(@"%@", string);
赋值
[string setString:@"人生没有彩排, 每天都是现场直播"]; NSLog(@"%@", string);
字符串拼接 "---<辉哥语录>"
[string appendString:@"---<辉哥语录>"]; NSLog(@"%@", string);
字符串的插入
NSMutableString *string1 = [[NSMutableString alloc]initWithFormat:@"我与有个约会"]; [string1 insertString:@"天使" atIndex:2]; NSLog(@"%@", string1);
字符串的删除
NSMutableString *string2 = [[NSMutableString alloc] initWithFormat:@"我想找个女汉子"]; [string2 deleteCharactersInRange: NSMakeRange(4, 1)]; NSLog(@"%@", string2);
字符串的替换
NSMutableString *string3 = [[NSMutableString alloc] initWithFormat:@"我想找个月薪1w的工作"]; [string3 replaceCharactersInRange:NSMakeRange(4, 1) withString:@"日"]; NSLog(@"%@", string3);