很多开发者都会有自己的一些小技巧
//创建一个字符串对象
NSString *str1 = @"www.hello.txt";
NSLog(@"%@",str1);
//用格式化字符串初始化
int a = 123;
NSString *str2 =[[NSString alloc]initWithFormat:@"%d %@",a,str1];
NSLog(@"%@",str2);
//快速创建一个字符串常量
NSString *string1 = @"abc";
NSString *string2 = @"cde";
NSLog(@"%@ %@",string1,string2);
//创建一个格式化的字符串 (内存在堆区)
NSString *string3 = [[NSString alloc]initWithFormat:@"整数: %d",10];
NSString *string4 = [[NSString alloc]initWithFormat:@"浮点数: %f",10.0];
NSLog(@"%@ %@",string3,string4);
//字符串比较
NSString *string5 = [NSString stringWithFormat:@"123"];
NSString *string6 = [NSString stringWithFormat:@"124"];
//测试内容是否相等
//方法一
if ([string5 isEqualToString:string6]) {
NSLog(@"same content");
}else{
NSLog(@"different content");
}
//方法二
//Bool : YES(1) NO(0)
BOOL rst = [string5 compare:string6];
NSLog(@"%d",rst);
//方法三
//不区分大小写比较字符串大小
NSString *aString = @"ABC";
NSString *otherString = @"abc";
NSComparisonResult rst3 = [aString compare:otherString];
NSLog(@"%ld",rst3);
//测试内容是否指向同一个对象
if (string5 == string6) {
NSLog(@"same pointer");
}else{
NSLog(@"difference pointer");
}
//求字符串长度
NSLog(@"%ld",[string6 length]);
//截取字符串中的字符
NSLog(@"%c",[string6 characterAtIndex:2]);
//字符串转换
NSString *string7 = @"hEllo world";
NSLog(@"%@",[string7 uppercaseString]);
NSLog(@"%@",[string7 lowercaseString]);
NSLog(@"%@",[string7 capitalizedString]);
//转换成基本数据类型
NSString *string8 = @"3.14";
float pi = [string8 floatValue];
NSLog(@"%f",pi);
//将oc字符转化为c的字符
const char *p = [string8 UTF8String];
NSLog(@"%c",*p);
//将字符串转换为数组
NSString *string9 = @"abc bcd xyz";
NSArray *array = [string9 componentsSeparatedByString:@" "];
NSLog(@"array: %@",array);
//截取字符串
NSString *string10 = @"abcdefg";
NSString *subString1 = [string10 substringToIndex:2];
NSLog(@"%@",subString1);
NSString *subString2 = [string10 substringFromIndex:2];
NSLog(@"%@",subString2);
//通过查找截取
NSRange range = {4,2};
range.location = 4;
range.length = 2;
//在range指定范围内抽取
NSString *subString3 = [string10 substringWithRange:range];
NSLog(@"%@",subString3);
//NSMakeRange
NSString *subString4 = [string10 substringWithRange:NSMakeRange(4, 3)];
NSLog(@"%@",subString4);
//字符串拼接
NSString *string11 = @"abc";
NSString *string12 = @"xyz";
NSString *appString1 = [[NSString alloc]initWithFormat:@"%@%@",string11,string12];
NSString *appString2 = [string11 stringByAppendingString:string12];
NSString *appString3 = [string11 stringByAppendingFormat:@"%@",string12];
NSLog(@"%@ %@ %@",appString1,appString2,appString3);
//查找字符串对象
NSString *link = @"abcdetarget=_blankxyz";
//查找子串,找不到返回
NSRange range1 = [link rangeOfString:@"target=_blank"];
if (range1.location != NSNotFound) {
NSLog(@"location = %ld,length = %ld",range1.location,range1.length);
}
//判断字符串是否以指定字符串开头
BOOL rst5 = [string1 hasPrefix:@"www"];
NSLog(@"%d",rst5);
//判断字符串是否以指定字符串结尾
BOOL rst6 = [string1 hasSuffix:@".txt"];
NSLog(@"%d",rst6);
//可变字符串对象:NSMutableString继承至string
NSMutableString *mutableString1 = [[NSMutableString alloc]initWithFormat:@"abc"];
NSLog(@"%@",mutableString1);
//对可变字符串赋值
[mutableString1 setString:@"qianfeng"];
NSLog(@"%@",mutableString1);
//插入一个字符串,在指定下标(不要越界)位置插入NSString类型字符
[mutableString1 insertString:@"...xyz" atIndex:3];
NSLog(@"%@",mutableString1);
//在字符串末尾追加字符串
[mutableString1 appendString:@"123"];
NSLog(@"%@",mutableString1);
//替换
NSRange range2={1,3};
[mutableString1 replaceCharactersInRange:range2 withString:@"efg"];
NSLog(@"%@",mutableString1);
//删除
[mutableString1 deleteCharactersInRange:range2];
NSLog(@"%@",mutableString1);