#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//----------------NSString-----------------------------
NSString *str1 = @"这是一个字符串常量";
NSLog(@"str1 = %@",str1);
//设置,相当于覆盖
//创建一个空字符串
NSString *str2 = [[NSString alloc]init];
NSString *str3 = [NSString string];//这俩种方法适用于数组 字典 集合
NSLog(@"str2 = %@",str2); //空的字符串
NSLog(@"str3 = %@",str3);
//快速创建一个字符串
NSString *str4 = [[NSString alloc]initWithString:str1];
NSString *str5 = [NSString stringWithString:str1];
NSLog(@"str4 = %@",str4);
NSLog(@"str5 = %@",str5);
//创建一个格式化字符串
NSString *str6 = [[NSString alloc]initWithFormat:@"%d",10];
NSString *str7 = [NSString stringWithFormat:@"%.2f",10.8099887];
NSLog(@"str6 = %@",str6);
NSLog(@"str7 = %@",str7);
//比较两个字符串内容是否相等
if ([str6 isEqualToString:str7]) { //相等则返回的为1真 返回BOOL类型
NSLog(@"str6 is EqualToString str7");
}else{ //不相等则返回的为0假
NSLog(@"str6 is not EqualToString str7");
}
//NSComparisonResult 数据类型
//比较结果分为三种,升序NSOrderedAscending,代表-1
//降序NSOrderedDecending,代表1
//相等NSOrderedSame,代表0
//转换为基本数据类型
NSString *str8 = @"3";
float f1 = [str8 floatValue];
NSInteger f2 = [str8 intValue];
NSLog(@"str8 = %.2f, f2 = %ld",f1,f2);
//字符串拆分为数组
NSString *str9 = @"命 运 就 算 颠 沛 流 离";
NSArray *array = [str9 componentsSeparatedByString:@" "];//以空格隔开算一个元素
NSLog(@"array = %@",array);
NSLog(@"array[0] = %@",array[1]);//运
//截取字符串 substring
NSString *str10 = @"123456789";
NSString *subStr1 = [str10 substringToIndex:4];//截取到第四个 1234
NSString *subStr2 = [str10 substringFromIndex:6];//从第几个下标开始截 789
NSLog(@"subStr1 = %@",subStr1);
NSLog(@"subStr2 = %@",subStr2);
NSRange range = {4,2};
NSString *subStr3 = [str10 substringWithRange:range];//从第四个下标 截取俩个 56
NSLog(@"subStr3 = %@",subStr3);
//拼接字符串
NSString *str11 = @"123";
NSString *str12 = @"456";
NSString *appStr1 = [[NSString alloc]initWithFormat:@"%@%@",str11,str12];
NSString *appStr2 = [str11 stringByAppendingFormat:@"%@",str12];
NSString *appStr3 = [str11 stringByAppendingString:str12];
NSLog(@"appStr1 = %@",appStr1);//初始化的一种
NSLog(@"appStr2 = %@",appStr2);//str11拼接str12格式化输入
NSLog(@"appStr3 = %@",appStr3);//str11拼接str12 直接传字符串名
//查找字符串
NSString *str13 = @"qweradsfzxcvqazxsw";
NSRange range1 = [str13 rangeOfString:@"dsfzx"]; //输入这个起始所在的下标及长度{5,5}结构体的形式输出 NSRange
NSLog(@"%@",NSStringFromRange(range1));//{5, 5}
NSLog(@"%ld",range1.location); //rang1字符串第一个下标5
if (range1.location != NSNotFound) {
NSLog(@"str found"); //用于判断A字符串是否在B字符串中
}else{
NSLog(@"str not found");
}
/*------------NSMutableString 可变字符串-----------------*/
//插入
NSMutableString *mStr1 = [[NSMutableString alloc] initWithFormat:@"abcd0"];
[mStr1 setString:@"abcd"];//设置,相当于覆盖 只能用于可变字符串
[mStr1 insertString:@"defg" atIndex:4]; //注意:此处为在源字符串上修改,并未生成新的字符串
NSLog(@"mStr1 = %@",mStr1); //在某下标后插入某字符串
//替换
[mStr1 replaceCharactersInRange:NSMakeRange(4, 4) withString:@"1234"];
NSLog(@"%@",mStr1);//从第四个下标开始替换四个 abcd1234
// NSString *result2 = [mStr1 stringByReplacingOccurrencesOfString:@"abcd" withString:@"1234"];abc替换成1234
//删除
[mStr1 deleteCharactersInRange:NSMakeRange(4, 4)];
NSLog(@"%@",mStr1); //从第四个下标开始删除四个
}
return 0;
}
//判断字符串是否为空
nil只是代表创建了一个空的对象
@""创建一个空得实例变量
所以判断字符串是否为空最好用字符串的长度是否为0来判断