8月17日NSString
一、字符串的创建
- //1.创建常量字符串
- NSString *str1 = @"hello world";
- NSLog(@"%@",str1);
- //2.通过类方法创建字符串
- NSString *str2 = [NSString stringWithString:str1];
- NSLog(@"str2 = %@",str2);
- //3.通过实例方法创建字符串
- NSString *str3 = [[NSString alloc]initWithString:str1];
- NSLog(@"str3 = %@",str3);
- //4.通过格式化方式创建字符串
- NSString *str4 = [NSString stringWithFormat:@"user = %@,pwd = %@",@"xu",@"123456"];
- NSLog(@"str4 = %@",str4);
- //5.通过C语言字符串创建
- const char *str5 = "how are you";
- NSString *str6 = [NSString stringWithUTF8String:str5];
- NSLog(@"str6 = %@",str6);
- //6.通过文件方式创建字符串
- NSError *error;
- NSString *str7 = [NSString stringWithContentsOfFile:@"/Users/mac/Desktop/OC进行时/day06/01-NSString/1.txt" encoding:NSUTF8StringEncoding error:&error];
- if (!error) {
- NSLog(@"str7 = %@",str7);
- }
- else
- {
- NSLog(@"文件读取失败:%@",error);
- }
- //7.通过URL方式创建字符串
- NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
- NSString *str8 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
- if (!error) {
- NSLog(@"str8 = %@",str8);
- }
- else
- {
- NSLog(@"文件读取失败:%@",error);
- }
二、字符串的比较
- NSString *str1 = @"hello world";
- NSString *str2 = @"how are you";
- NSString *str3 = [NSString stringWithString:str1];//使用已有的字符串创建新的字符串
- //方式1
- if (str1 == str3){
- NSLog(@"str1 == str3");
- }
- //方式2
- if ([str1 isEqual:str3]) {
- NSLog(@"str1 == str3");
- }
- //方式3
- if ([str1 isEqualTo:str3]) {
- NSLog(@"str1 == str3");
- }
- //方式4
- if([str1 isEqualToString:@"hello world"])
- {
- NSLog(@"str1 == str3");
- }
- //方式5
- NSComparisonResult result = [str1 compare:str2];
- if (result == NSOrderedSame) {
- NSLog(@"str1 == str2");
- }
- else if (result == NSOrderedAscending)
- {
- NSLog(@"str1 < str2");
- }
- else//NSOrederDescending
- {
- NSLog(@"str1 > str2");
- }
- /*
- compare的是一个枚举类型的,如果返回值为:-1表示NSOrderedAscending;返回值为0:NSOrderedSame;返回值为1:NSOrderedDescending
- */
三、字符串检索
- NSString *str1 = @"how are you";
- NSRange range = [str1 rangeOfString:@"are"];
- NSLog(@"location = %ld,length = %ld",range.location,range.length);
- NSString *str2 = @"http://www.baidu.com”;
- //判断字符串是否以某前缀开头
- if ([str2 hasPrefix:@"http://"]) {
- NSLog(@"http协议");
- }
- //判断字符串是否以某后缀结尾
- NSString *str3 = @"/users/destTop/1.txt";
- if ([str3 hasSuffix:@"txt"]) {
- NSLog(@"文本文件");
- }
- NSString *str4 = @"http://www.baidu.com?user=admin&pwd=123456";
- NSRange range1 = [str4 rangeOfString:@"user="];
- NSRange range2 = [str4 rangeOfString:@"pwd="];
- NSUInteger location = range1.location + range1.length;
- NSUInteger length = range2.location-location;
- NSRange range3 = NSMakeRange(location, length);
- NSString *user = [str4 substringWithRange:range3];
- NSString *pwd = [str4 substringFromIndex:range2.location + range2.length];
- NSLog(@"user = %@,pwd =%@",user,pwd);
四、其他类型转换字符串
- NSString *str1 = [NSString stringWithFormat:@"%d,%.2f,%c,%s",10,12.34,'a',"hello world"];
- NSLog(@"str1 = %@",str1);
- //1.字符串转整型
- NSString *str2 = @"20";
- NSInteger i = [str2 integerValue];
- NSLog(@"i = %ld",i);
- //2.字符串转实型
- CGFloat j = [str2 doubleValue];
- NSLog(@"j = %.2lf",j);
- //3.字符串转C语言字符串
- NSString *str4 = @"how are you";
- const char *pStr = [str4 UTF8String];
- NSLog(@"pStr = %s",pStr);
五、可变字符串的常用操作
//1.创建
NSMutableString *str = [NSMutableString stringWithCapacity : 容量];
//2.添加内容
[str appendSting:@“hello world”];
[str appendFormat:@“%d”,123];
//这两种插入方式都在尾部进行插入;
//3.插入内容:
[str insertString:@“ok “ atIndex:插入位置];
//4.替换内容
NSRange range = [str rangeOfString:@“world”];
[str replaceCharactersInRange:range withString:@“WORLD”];
//5.删除内容
[str deleteCharactersRange:range];
//6.设置成其他字符串
[str setString:@“how are you”];
六、自定义结构体
//1、创建一个Student为例:
- typedef struct
- {
- char *name;
- int age;
- char gender;
- }Student;
- Student stu = {“zhangsan”,22,’F’};
- NSValue *value = [NSValue valueWithBytes:&stu objcType:@encode(Student)];
- NSLog(@“name = %s,age = %d,gender = %c”,stu.name,stu.age,stu.gender);
- Student stu2;
- [value getValue:&stu2];
- NSLog(@“name = %s,age = %d,gender = %c”,stu2.name,stu2.age,stu2.gender);
/* 封装自定义结构体的操作:
1.声明结构体变量
2.创建一个NSValue的值对象,并将结构体变量的地址存放进去,
3.也可以通过getValue将值对象存储的数据存放到另外一个结构体变量中。
*/
2、在Foundation下的常用结构体NSRange、NSPoint、NSSize、NSRect
//1.创建一个结构体变量,用来确定范围
- NSRange range = NSMakeRange(0,5);
- //2.创建一个值对象
- NSValue *value = [NSValue valueWithRange:range];
- //3.取出这个值对象的值
- NSRange range2 = [value rangeValue];
NSPoint、NSSize、NSRect与NSRange的操作雷同,不再一一列举。
七、NSArray
//1、创建
- NSArray *arr1 = @[@"one",@"two",@"three",@"four”];
- //通过对象
- NSArray *arr2 = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four", nil];
- NSArray *arr3 = [[NSArray alloc]initWithObjects:@"one",@"two",@"three",@"four", nil];
- NSArray *arr4 = [[NSArray alloc]initWithArray:arr3];
//2、遍历
- //第一种:
- for (int i = 0; i<[arr2 count]; i++) {
- NSLog(@"arr2->%@",arr2[i]);
- }
- //第二种:
- for (int i = 0; i < [arr2 count]; i++) {
- NSLog(@"arr2->%@",[arr2 objectAtIndex:i]);
- }
- //第三种:
- [arr2 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- NSLog(@"index = %ld,obj = %@",idx,obj);
- if ([obj integerValue] == 3) {
- *stop = YES;
- }
- }];
- //第四种:
- for(id obj in arr2)
- {
- NSLog(@"%@",obj);
- }