//创建字符串的方式
NSString*str3 =@"iOS";//系统会对内存自动做管理
NSString *str4 = [[NSString alloc]init];//通过该方式进行创建字符串则需要手动释放内存。
str4 =@"iOS";
-----------------------------------------------------------------------------------------------------------------
//格式化字符串
int c = 10;
int d = 20;
NSString *str5 = [NSString stringWithFormat:@"c=%d d=%d",c,d];
NSLog(@"str5 =%@",str5);
-----------------------------------------------------------------------------------------------------------------
//拼接字符串
NSString *str6 =[str5 stringByAppendingString:str3];
NSLog(@"str6 =%@",str6);
-----------------------------------------------------------------------------------------------------------------
//大小写转换
//大转小
NSString*str7 =@"aBcDeFg";
NSString*str8 = [str7lowercaseString];
NSLog(@" str8 =%@",str8);
//小转大
NSString*str9 = [str7uppercaseString];
NSLog(@"str9 = %@",str9);
-----------------------------------------------------------------------------------------------------------------
//前缀和后缀判断
NSString*str10 =@"www.baiud.com";
// 判断前缀
BOOLhasPreFix = [str10hasPrefix:@".com"];
if(hasPreFix) {
NSLog(@"有对应前缀");
}else{
NSLog(@"没有对应前缀");
}
//判断后缀
BOOLhasSuffix = [str10hasSuffix:@"www."];
if(hasSuffix) {
NSLog(@"有对应后缀");
}else{
NSLog(@"没有对应后缀");
}
-----------------------------------------------------------------------------------------------------------------
//分割
//判断两个字符串是否相同
NSString*str11 =@"hello";
NSString*str12 =@"hello";
if([str11isEqualToString:str12])
NSLog(@"两个字符串一致");
else
NSLog(@"两个字符串不一致");
-----------------------------------------------------------------------------------------------------------------
// 比较字符串
// NSComparisonResult
-----------------------------------------------------------------------------------------------------------------
// 按照指定字符分割字符串
NSString*str13 =@"a,b,c,d,e,f,g";
NSArray *strArray = [str13 componentsSeparatedByString:@","];
for(NSString*strinstrArray) {
NSLog(@"str = %@",str);
}
-----------------------------------------------------------------------------------------------------------------
//按照范围截取 字符串
NSRangerange =NSMakeRange(1,5);
NSString*str14 = [str13substringWithRange:range];
NSLog(@"str14 = %@",str14);
-----------------------------------------------------------------------------------------------------------------
//从某一位开始截取后面的字符串
NSString*str15 = [str13substringFromIndex:2];
NSLog(@"str15 = %@",str15);
-----------------------------------------------------------------------------------------------------------------
// 从开头截取到某一位
NSString*str16 = [str13substringToIndex:7];
NSLog(@"str16 = %@",str16);
-----------------------------------------------------------------------------------------------------------------
//将字符串拆分为每一个字符
for(inti=0; i<[str13length]; i++) {
NSLog(@"%c",[str13characterAtIndex:i]);
}
-----------------------------------------------------------------------------------------------------------------
NSString *str17 = @"ab cd ef gh ij ab";
//查找指定字符串的位置
NSRangerange1 = [str17rangeOfString:@"ab"];
NSLog(@"range1.loaction:%ld range1.length:%ld",range1.location,range1.length);
-----------------------------------------------------------------------------------------------------------------
//替换
//替换某一个范围的内容
NSString *str18 =@"baidu,alipay,apple,facebook";
NSLog(@"str18 =%@",str18);
NSString *str19 = [str18 stringByReplacingCharactersInRange:NSMakeRange(0,5) withString:@"xcode"];
NSLog(@"str19 =%@",str19);
-----------------------------------------------------------------------------------------------------------------
//用指定字符串题号源字符串的字串
NSString *str20 = [str18 stringByReplacingOccurrencesOfString:@"alipay" withString:(@"hello")];
NSLog(@"str20 = %@",str20);
-----------------------------------------------------------------------------------------------------------------