int a = 0;
//字符串
//1.创建方法 初始化方法;用alloc init调用
//产生一个字符串
NSString * str = [[NSString alloc]initWithFormat:@"asdasd随便%d",a];
//构造器;用类调用
NSString * str1 = [NSString stringWithFormat:@"哈哈哈哈哈哈哈%d",a];
NSLog(@"%@,%@",str,str1);
//字符串长度
NSLog(@"%lu,%lu",[str length],[str1 length]);//计算字符串长度
//追加方法
//字符串的拼接
//在字符串的方法中,只要碰到fromat关键词,都可以在后面的参数中拼接 参数
//Format:可以拼接%d;String:不能拼接%d
NSString * strAppend = [str stringByAppendingFormat:@"啊啊啊啊%d",a];//可以拼接%d
NSLog(@"%@",strAppend);
NSString * sttAppend = [str stringByAppendingString:@"呵呵"];//不能拼接%d
NSLog(@"%@",sttAppend);
NSString * abb = [[NSString alloc]initWithFormat:@"啦啦啦啦啦啦啦啦啦啦啦"];
NSLog(@"%@",abb);
//取子串
//子串关键词:substring
//按照字符串中每个字符所在的下标
//从第几个开始取后面的字符串
NSString * subStr1 = [abb substringFromIndex:3];
NSLog(@"sbuStr1 = %@",subStr1);
//从头开始取到第几个字符,后面的都不要
NSString * subStr2 = [abb substringToIndex:3];
NSLog(@"sbuStr2 = %@",subStr2);
//NSRange是一个标记范围的结构体
//两个成员变量 : {从什么位置开始,在开始的位置之后取得几个字符}
NSRange range = {1,3};
NSString * subStr3 = [abb substringWithRange:range];//从第几个开始,取几个值;从'a'开始取,取'三个啦啦啦'
NSLog(@"subStr3 = %@",subStr3);
NSString * str = [[NSString alloc]initWithFormat:@"%d",a];
NSLog(@"%d",str);
//字符串替换
//关键字replace
//把字符串里的0替换为零
//第一个参数:要被替代的字符串
//第二个参数:替换成的字符串
NSString * replaceStr = [str stringByReplacingOccurrencesOfString:@"0" withString:@"零"];
NSLog(@"%@",replaceStr);
NSString * abb3 = [[NSString alloc]initWithFormat:@"啦啦啦啦啦啦啦啦啦啦啦"];
//把一段范围内的所有字符串替换为@"噢噢噢"
NSString * replaceAbb3 = [abb3 stringByReplacingOccurrencesOfString:@"啦啦啦啦啦啦啦啦啦啦啦" withString:@"噢噢噢"];
NSLog(@"%@",replaceAbb3);
//range:从第几个开始替换,范围是把几个字符替换成第二个参数
NSRange range1 = {1,3};
NSString * replaceAbb3 = [abb3 stringByReplacingCharactersInRange:range withString:@"11111"];
NSString * abb4 = [[NSString alloc]initWithFormat:@"abcabcabcabcabcabcabc"];
//在给定的范围(range2)内,把所有的 @"a" 替换为 @"零"
NSRange range1 = {0,4};
NSString * replaceStt2 = [str stringByReplacingOccurrencesOfString:@"a" withString:@"零" options:NSCaseInsensitiveSearch range:range1];
NSLog(@"%@",replaceStt2);
//字符串转换为其他类型
//将字符串转为float类型
NSString * strNum = [NSString stringWithFormat:@"132ad121"];
float num = [strNum floatValue];
NSLog(@"%@ 转换为 %.2f",strNum,num);
//字符串比较
//判断字符串是否一样
NSString * strCompare1 = @"啊啊啊";
NSString * strCompare2 = @"噢噢噢";
//判断两个字符串是不是一样
BOOL c = [str isEqualToString:strNum];
NSLog(@"%@",c?@"YES":@"NO");//BOOL类型打出YES OR NO
//判断字符串之间的大小
//判断的结果分为三类
//1.str1 比 str2 小,这种情况称之为升序排列 result为 -1
//2.相等,result 为 0
//3.str1 比 str2 大,成为降序排列 result为 1
int result = [strCompare1 compare:strCompare2];
NSLog(@"%d",result);