1.用字面量的方式创建一个字符串的对象
NSString *str=@"文世倾wenshixuandhjhgfdg";
2.打印字符串的长度
NSLog(@“%ld”,str.length);
——->21
3.通过下标获取字符串的内容(打印的是汉字的话用”%C”)
NSLog(@"%c",[str characterAtIndex:4]);
——->e
4.用便利构造器的方法创建字符串对象
NSString *str=[NSString stringWithString:@“文世轩”];
//NSString *str=@“niahao,sadfaerzhty,ujk,m";
5.打印指定下标到结尾的字符串内容
NSLog(@"%@",[str substringFromIndex:5]);
——->o,sadfaerzhty,ujk,m
打印开头到指定下标的字符串内容
NSLog(@"%@",[str substringToIndex:5]);
———>niaha
6.字符串截取
(1)NSRange rang={3,7};
NSLog(@“%@“,[str strsubstringWithRange:rang]);
nsstring *s=[str strsubstringWithRange:rang]
NSLog(@“%@“,s);
———>hao,sad
(2)NSLog(@"%@",[str substringWithRange:NSMakeRange(3, 7)]);
——->hao,sad
7.判断两个字符串是否相等
NSString *str1=@"文世轩";
NSString *str2=@"文世倾";
NSLog(@"%d",[str1 isEqualTo:str2]);
——->0
8. NSString *str=@"打了八只蚊子";
NSString *prefix=@"打了";
NSString *prefix1=@"蚊子";
(1)前缀是否是”打了”
NSLog(@"%d",[str hasPrefix:prefix]);
——->1(不是为0)
(2)后缀是否是”蚊子”
NSLog(@"%d",[str hasSuffix:prefix1]);
——->1(不是为0)
9.把字符串变成基本数据类型
NSString *str=@"12345";
NSInteger num=[str integerValue];
NSLog(@"%ld",num);
——->12345
10.把任意类型转换成字符串类型
CGFloat g=3.14;
NSInteger a=100;
NSString *str=[NSString stringWithFormat:@"%ld,%g,%@",a,g,@"ningzhiyuan"];
NSLog(@"%@",str);
----
11.(1)将所有小写字母变大写
NSString *str=@"DFD123laj汉字kjdss";
NSLog(@"%@",[str uppercaseString]);
——->DFD123LAJ汉字KJDSS
(2)将所有大写转换成小写
NSString *str1=@"SfdhgDKFJDLF";
NSLog(@"%@",[str1 lowercaseString]);
——->dfd123laj汉字kjdss
(3)首字母大写
NSLog(@“%@",str.capitalizedString);
———>Dfd123Laj汉字Kjdss
12.字符串比较
NSString *str=@"nihao";
NSString *newstr=@"buhao";
NSLog(@"%ld",[str compare:newstr]);
——->1
13.字符串的拼接
NSString *str=@"sadjfkasd";
NSString *str1=@"askjzkncm63210";
NSString *newstr=[str stringByAppendingString:str1];
NSLog(@“%@",newstr);
———>sadjfkasdaskjzkncm63210
14.替换
NSString *str=@"12312133";
NSString *str1=[str stringByReplacingOccurrencesOfString:@"12"withString:@"*"];
NSLog(@“%@",str1);
——->*3*133
可变字符串
1.创建
(1)NSMutableString *str=[[NSMutableString alloc] initWithString:@"anyichen"];
NSLog(@“%@",str);
——->anyichen
(2)NSMutableString *str1=[NSMutableString stringWithString:@"wenshixuan"];
NSLog(@“%@",str1);
———>wenshixuan
2.插入
NSMutableString *str=[NSMutableString stringWithString:@"anyihcen"];
[str insertString:@"安逸臣" atIndex:3];
NSLog(@“%@“,str);
——->any安逸臣ihcen
3.替换
NSMutableString *str1=[NSMutableString stringWithString:@"anyichen"];
[str1 replaceCharactersInRange:NSMakeRange(3, 3) withString:@“宁致远"];
NSLog(@“%@",str1);
——->any宁致远en
4.字符串拼接
(1)NSMutableString *str=[NSMutableString stringWithString:@"xiaoyahuizi"];
[str appendString:@"akjf"];
NSLog(@“%@“,str);
——->xiaoyahuiziakjf
(2)NSMutableString *str1=[NSMutableString stringWithString:@"anleyan"];
NSString *str2=@"sajfkl";
NSString *newstr=[str1 stringByAppendingString:str2];
NSLog(@“%@",newstr);
——-> anleyansajfkl
NSSet 集合
(1)便利构造器创建
NSSet *set=[NSSet setWithObjects:@"1",@"2",@"3",@"4",@"5",@"2", nil];
(2)一般创建
NSSet *set1=[[NSSet alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
(3)可变集合
NSMutableSet *set=[NSMutableSet setWithObjects:@"1",@"2",@"4",@"3",@"5", nil];
(4)添加元素
[set addObject:@“6"];
(5)删除元素
[set removeObject:@“1"];
(6)清除所有
[set removeAllObjects];
(7)快速枚举
for (NSString *str in set) {
NSLog(@“%@",str);}
NSDate日期的类
NSDate *date=[NSDate date];
(1)获取一下当前所在时区,
NSTimeZone *zone=[NSTimeZone systemTimeZone];
(2)获取一下和0时区相差的秒数
NSInteger second=[zone secondsFromGMTForDate:date];
(3)通过相差的秒数,能获取到现在的时间
NSDate *localdate=[NSDate dateWithTimeIntervalSinceNow:second];
(4)明天此时的时间
NSDate *tomorrowDate =[NSDate dateWithTimeIntervalSinceNow:(32*3600)];
NSLog(@"%@",tomorrowDate );
(5)昨天此时的时间
NSDate *yesterday=[NSDate dateWithTimeIntervalSinceNow:-(16*3600)];
NSLog(@"%@",yesterday);
(6)时间间隔
对应的double类型
计算两个时间对象的时间间隔
NSTimeInterval interval=[tomorrowDate timeIntervalSinceDate:date];
NSLog(@"%g",interval);
(7)NSDate->NSString
第一种方式
NSDate *date=[NSDate date];
NSString *dateStr=[NSString stringWithFormat:@"%@",date];
第二种方式,时间的格式yyyy-MM-dd HH-mm-ss
y-年,M-月,d-日,H-24小时制,,h-12小时制,,m-分,,s-秒
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
NSDate *date=[NSDate date];
NSString *strDate=[formatter stringFromDate:date];
字符串->NSDate
//使用前要先定义格式
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
NSString *timeStr=@"2015-7-23 17-18-10";
NSDate *date=[formatter dateFromString:timeStr];
一.block的四种形式
1.无参数,无返回值
void(^block)()=^(){
NSLog(@“今天正热”);
};
block();
——->今天正热
2.有参数,无返回值
void(^block)(int, int)=^(int a, int b){
int max=a>b?a:b;
NSLog(@“%d”,max);
};
block(10,20);
———>20
void(^block)(NSString *)=^(NSString *str){
NSLog(@“%@”,str);
};
block(@“nihao”);
———->nihao
3.有返回值,无参数
int (^block)()=^(){
return (int)100;
};
NSLog(@“%d”,block());
4.有参数,有返回值
NSString *(^block)(NSArray *,NSInteger)=^(NSArray *arr,NSIteger index){
return arr[index];
};
block(@[@“1”,@“2”,@“3”,@“4”],1);
———->2
数组的遍历
1.block 遍历数组
NSArray *arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@“%@",obj);
}];
2.forin
NSArray *arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
for (NSString *str in arr) {
NSLog(@"%@",str);
}
3.for
NSArray *arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
for(NSInteger i=0;i