main.m
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
1.API 应用编程接口 Application Programming Interface.
2.SDK 软件开发工具包 Software Development kit.
3.IDE 集成开发环境 Intergrated Development Environment
}
1. NSString 不可变字符串.
NSString *str1 = [[NSString alloc] initWithString:@"一个字符串"];
[str1 release];
NSString *str2 = [NSString stringWithSting@"还是一个字符串"]; // 现在不这么写了.
2. 字面量.
NSString *str3 = @"这才是创建字符串!";
NSLog(@“%@“, str3);
NSString *str4 = @"困困困";
3. 字符串拼接.
NSString *str5 = [NSString stringWithFormat:@"%@%@%ld", str3, str4, 100ul]; // ul代表无符号长整形.
NSLog(@"%@", str5);
4. 万能公式.
(1). int -> string
NSString *str6 = [NSString stringWithFormat:@"%ld", 50L];
NSLog(@"%@", str6);
(2). string -> int
NSString *str7 = @"123456";
NSInteger num7 = [str7 integerValue]; // 从头往后数,如果遇到除数字之外的东西就将遇到的数字打印出来,如果没有遇到数字就打印0.
NSLog(@"%ld", num7);
5. 求字符串的长度.
NSString *str8 = [NSString stringWithFormat:@“哈哈哈哈哈”];
NSLog(@"%lu", str8.length); // OC中编码是用ASCII码.
NSLog(@"%lu", strlen(“哈哈哈哈哈”)); // C中编码是用Unicode,在此注意OC和C的中文字节数(英文没有区别).
NSString *str9 = [NSString stringWithFormat:@"adgasdgdsagdsaf"];
NSLog(@"%lu", str9.length);
6. 取子字符串.
(1).
NSString *str10 = @"哈哈哈呵呵呵嘿嘿嘿";
NSString *subStr1 = [str10 substringFromIndex:3]; // 从第几位开始一直取到最后.
NSLog(@"%@",subStr1);
(2).
NSString *subStr2 = [str10 substringToIndex:2]; // 从头开始取到第几位取完.
NSLog(@"%@", subStr2);
(3).
NSString *subStr3 = [str10 substringWithRange:NSMakeRange(3, 3)]; // 范围截取.
NSLog(@"%@", subStr3);
7. 判断字符串,前缀,后缀,中间.
NSString *strZB = @"SuperIronZombie";
(1).前缀
if([strZB hasPrefix:@"S"]) {
NSLog(@"有前缀!~");
} else {
NSLog(@"没有前缀!~");
}
(2).后缀
if([strZB hasSuffix:@"e"]) {
NSLog(@"有后缀!~");
} else {
NSLog(@"没有后缀!~");
}
(3).中间
if ([strZB containsString:@"I"]) {
NSLog(@"有有有!~");
} else {
NSLog(@"没没没!~");
}
(4).相等
NSString *strZC = @"SuperIronZombie";
// strZB == strZC; 这种形式不能相等,因为地址不同.
if ([strZB isEqualToString:strZC]) {
NSLog(@"相等!~");
} else {
NSLog(@"没有!~");
}
8.字符串拼接
NSString *str1 = @"iPhone";
NSString *str2 = [[str1 stringByAppendingString:@"6s"] stringByApperndingString:@"is too smart"];
NSLog(@"%@", str2);
9.字符串替换
参数1. target要替换的部分.
参数2. replacement 替换成什么.
NSString *str3 = [str2 stringByReplacingOccurrencesOfString:@"a" withString:@"small"];
NSLog(@"%@", str3);
10.字符串比较
NSString *str4 = @"iPhone4s";
NSString *str5 = @"iPhone6s";
NSLog(@"%ld", (long)[str4 compare:str5]);
NSComparisonResult r = [str4 compare:str5];
NSLog(@"%ld", r);
11. NSMutableString 可变字符串.
NSMutableString *MS1 = [NSMutableString stringWithCapacity:0];
NSMutableString *MS2 = [NSMutableString alloc] initWithCapacity:0];
NSMutableString *MSsuper = [NSMutableString string]; //因为NSMutableString继承于NSObject,所以这是父类中的方法.
(1).拼接
[MSsuper appendString:@"字符串"];
NSLog(@"%@", MSsuper);
(2).插入
[MSsuper insertString:"哈哈" atIndex:1];
NSLog(@"%@", MSsuper);
(3).删除
[MSsuper deleteCharactersInRange:NSMakeRange(1, 2)];
NSLog(@"%@", MSsuper);
12.可变字符串调用不可变字符串的方法.
(1).
[MSsuper stringByReplacingOccurrencesOfString:@"符" withString:@"啦啦"]; // 本方法有返回值所以必须有对象来接收,以上句子报错.
NSLog(@"%@", [MSsuper stringByReplacingOccurrencesOfString:@"符" withString:@"啦啦"]); // 直接放到NSLog中,就相当于把值打印了.
(2).
可变字符串一旦调用了不可变字符串的方法,他的返回值一定是不可变的.
NSString *str1 = [MSsuper stringByReplacingOccurrencesOfString:@"符" withString:@"啦啦"];
13. 以下三种输出的结果,是整型的最大值.
(1).
NSString *str = @"iphone";
NSLog(@"%lu", str.retainCount);
(2).
NSString *str1 = [[NSString alloc] initWithString:@"iphone"];
NSLog(@"%lu", str1.retainCount);
(3).
NSString *str2 = [NSString stringWithString:@"iphone"];
NSLog(@"%lu", str2.retainCount);
14.不可变字符串的引用计数为-1(最大值)
NSString *str3 = [NSString string];
NSLog(@"%ld", str3.retainCount);
NSString *str4 = [NSString stringWithFormat:@"iphone"];
NSLog(@"%ld", str4.retainCount);
NSString *str5 = [[NSString alloc] initWithFormat:@"iphone"];
NSLog(@"%ld", str5.retainCount);
str5 = [str4 retain]; // 此语句证明不可变字符串的引用计数是不可变的.
NSLog(@"%ld", str5.retainCount);
15.可变字符串,遵循引用计数的规律
NSMutableString *mstr3 = [NSMutableString string];
NSLog(@"%ld", mstr3.retainCount);
16.大小写的形式
(1). 首字母大写
NSString *str = @"suPer man";
NSLog(@"%@", [str capitalizedString]);
(2). 全部大写
NSLog(@"%@", [str uppercaseString]);
(3). 全部小写
NSLog(@"%@", [str lowercaseString]);