标签:
//1. Xcode->Help->Decumentation and API
//快捷键: cmd + shift + 0
//使用场景: 无目的的自主学习
//2. 快速进入API文档
//快捷键:cmd + 左键
//使用场景: 快速浏览头文件, 对于这个类有一定了解, 比如, 忘了一个方法的名字
//3. 快速了解一个类
//快捷键: alt + 左键
//使用场景: 快速查看一个类的overview, 如果也想要深入了解, 可以快速进入API文档
//NSString: 不可变字符串, 一旦创建, 不能修改
//初始化方法
//创建空字符串
NSString *string1 = [[NSString alloc] init];
NSLog(@"%@", string1);
//根据给定的常量, 创建NSString对象
NSString *string2 = [[NSString alloc] initWithString:@"iphone"]; //创建常量区的字符串
//initWithFormat 创建堆区的字符串
NSLog(@"%@", string2);
//常量区字符串
NSString *string3 = @"iphone";
NSLog(@"%@", string3);
NSLog(@"%p", string3);
//如果要创建的字符串中出现其他变量, 需要用initWithFormat的方式创建.
NSInteger version = 6;
NSString *string4 = [[NSString alloc] initWithFormat:@"iphone %ld", version];
NSLog(@"%@", string4);
//便利构造器
NSString *str1 = [NSString string];
NSLog(@"%@", str1);
NSString *str2 = [NSString stringWithString:@"iphone"];
NSLog(@"%@", str2);
NSString *str3 = @"iphone";
NSLog(@"%@", str3);
NSString *str4 = [NSString stringWithFormat:@"iphone %ld", version];
NSLog(@"%@", str4);
//获取字符串的长度
NSUInteger length = [str4 length];
NSLog(@"%lu", length);
NSLog(@"%lu", str4.length); //点语法
//判断字符串是否以指定的字符串开始或结束
NSString *str5 = @"abcHHH123";//常量区字符串
NSString *str6 = [[NSString alloc] initWithFormat:@"abcHHH123"]; //堆区字符串
// 前缀
BOOL result = [str6 hasPrefix:@"abc"];
NSLog(@"%d", result);
//后缀
result = [str5 hasSuffix:@"123"];
NSLog(@"%d", result);
//字符串截取(获取子字符串)
//获取abc
NSString *subString1 = [str6 substringToIndex:3];
NSLog(@"%@", subString1);
//获取123
NSString *subString2 = [str6 substringFromIndex:str6.length - 3];
NSLog(@"%@", subString2);
//获取HHH
NSRange range = {3, 3};
NSString *subString3 = [str6 substringWithRange:range];
NSLog(@"%@", subString3);
NSString *subString4 = [str6 substringWithRange:NSMakeRange(3, 3)];
NSLog(@"%@", subString4);
//拼接字符串, 不会改变原有的字符串, 拼接好的字符串作为返回值
NSString *newString = [subString1 stringByAppendingString:subString2];
NSLog(@"%@", subString1);
NSLog(@"%@", subString2);
NSLog(@"%@", newString);
//方法2
NSString *newString1 = [NSString stringWithFormat:@"%@%@", subString1, subString2];
NSLog(@"%@", newString1);
//替换字符串
//@"NBA" -> @"CBA"
NSString *str7 = @"NBANNN";
NSString *newString2 = [str7 stringByReplacingOccurrencesOfString:@"N" withString:@"C"];
NSLog(@"%@", newString2);
//字符串比较
//@"NBA" 和 @"CBA"
// NSString *str8 = @"NBA";
// NSString *str9 = @"CBA";
NSString *str8 = [[NSString alloc] initWithFormat:@"NBAsfsfsfsrtr"];
NSString *str9 = [[NSString alloc] initWithFormat:@"NBAsfsfsfsrtr"];
//isEqualToString比较的是字符串的内容
if ([str8 isEqualToString:str9]) {
NSLog(@"字符串相等");
} else {
NSLog(@"字符串不相等");
}
//==比较的是两个对象是否一致, 如果对象一样,他们的内容也一样
if (str8 == str9) {
NSLog(@"字符串相等");
} else {
NSLog(@"不一致");
}
//字符串和数值类型转换
//@"123"
//转换成数字, 遇到非数字及停止转换
NSString *str10 = @"123abc";
int a = [str10 intValue];
NSLog(@"%d", a);
NSInteger b = [str10 integerValue];
NSLog(@"%ld", b);
//大小写转换操作
//@"ios" -> @"IOS"
NSString *str11 = @"ios";
NSString *newString3 = [str11 uppercaseString];
NSLog(@"%@", newString3);
//首字母大写
NSLog(@"%@", [str11 capitalizedString]);
//全部小写
NSLog(@"%@", [str11 lowercaseString]);
//NSMutableString:可变字符串
//@"iPhone"
//创建可变字符串,容器大小是一个预期值, 一般写为0, 让系统自己根据内容的大小分配空间
NSMutableString *mString = [[NSMutableString alloc] initWithCapacity:0];
NSLog(@"%@", mString);
//添加字符串
[mString appendString:@"iPhone"];
NSLog(@"%@", mString);
[mString appendFormat:@"iPhone %ld", version];
NSLog(@"%@", mString);
//插入字符串
[mString insertString:@" " atIndex:6];
NSLog(@"%@", mString);
//删除字符串
[mString deleteCharactersInRange:NSMakeRange(0, 7)];//0是起点,7是长度
NSLog(@"%@", mString);
//给定一个图片文件名, 判断字符串中是否以"png"结尾, 如果是就替换成"jpg", 如果不是, 就拼接".jpg"
NSMutableString *name = [[NSMutableString alloc] initWithFormat:@"shenghuo.php"];
if ([name hasSuffix:@"png"]) {
NSLog(@"以‘png‘结尾");
[name replaceCharactersInRange:NSMakeRange(name.length - 3, 3) withString:@"jpg"];
} else {
NSLog(@"不以‘png‘结尾");
[name appendString:@".jpg"];
}
NSLog(@"%@", name);
//NSArray,不可变数组
//一旦创建, 不能更改
//数组中存放的元素必须是对象类型的
//数组以nil为结束符号
//创建数组
NSArray *array = [[NSArray alloc] initWithObjects:@"abc", @"123", @"ABC", nil];
NSLog(@"%@", array);
Person *person1 = [[Person alloc] init];
person1.name = @"xiaoming";
person1.age = 18;
Person *person2 = [[Person alloc] init];
person2.name = @"xiaoqiang";
person2.age = 20;
NSArray *array1 = [[NSArray alloc] initWithObjects:person1, @"person2", nil];
NSLog(@"%@", array1);
//获取数组中的某个元素
Person *person3 = [array1 objectAtIndex:0];
NSLog(@"%@", person3);
//获取数组中的元素个数
NSLog(@"%lu", [array1 count]);
//NSMutableArray:可变数组
NSMutableArray *mArray = [[NSMutableArray alloc] initWithCapacity:0];
//添加元素
[mArray addObject:@"aaa"];
[mArray addObject:@"bbb"];
[mArray addObject:@"ccc"];
NSArray *array3 = [NSArray arrayWithObjects:@"aaa", @"bbb", @"ccc", nil];
[mArray addObjectsFromArray:array3];
NSLog(@"%@", mArray);
//修改某个元素
[mArray replaceObjectAtIndex:3 withObject:@"ddd"];
NSLog(@"%@", mArray);
//插入一个元素
[mArray insertObject:@"aaa" atIndex:4];
NSLog(@"%@", mArray);
//删除某个元素
[mArray removeObjectAtIndex:1];
NSLog(@"%@", mArray);
//数组排序
[mArray sortUsingSelector:@selector(compare:)];
NSLog(@"%@", mArray);
//交换两个元素
[mArray exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"%@", mArray);
//遍历数组
for (int i = 0; i < mArray.count; i++) {
NSLog(@"%@", [mArray objectAtIndex:i]);
}
//当数组中的元素类型一样时, 使用forin循环, 快速枚举
for (NSString *string in mArray) {
NSLog(@"%@", string);
}
//NSNumber:数值类型
//基本数据类型 转换成 NSNumber
NSNumber *num = [NSNumber numberWithInt:123];
NSLog(@"%@", num);
//NSNumber 转换成 基本数据类型
int d = [num intValue];
NSLog(@"%d", d);
标签:
原文地址:http://www.cnblogs.com/xiaoxuetongxie/p/4319284.html