NSString字符串常用知识点

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//1.字符串创建方法(最常用)

NSString *str = [[NSStringalloc]initWithFormat:@"LILILI"];

NSLog(@"str = %@",str);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//2.字符串追加

NSString *str1 = [[NSStringalloc]initWithFormat:@"%@%@",str,@"wwww"];

NSLog(@"str1 = %@",str1);

   }

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//3.求字符串长度

NSString *str2 = [[NSStringalloc]initWithFormat:@"str1%@%@",@"李明",@"str1"];

NSUInteger length = [str2 length];

NSLog(@"str2 = %ld",length);

NSLog(@"str2 = %@",str2);

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//4.求字串以及字符串中取某个字符(字符的截取)

//求字串方法三种

NSString *str3 = [[NSStringalloc]initWithFormat:@"LANOUHHH"];


NSString *a = [str3 substringFromIndex:2];//1.substringFromIndex,常常从此下标开始取他之后

NSLog(@"a = %@",a);

NSString *str4 = [[NSStringalloc]initWithFormat:@"fsefdefrgvf"];

NSString *b = [str4 substringToIndex:5];//2.substringToIndex常从此下标开始取他前面的字符也就是子串

NSLog(@"b = %@",b);

NSRange range = NSMakeRange(3, 4);//3.location  length  从下标为3开始取后四个

NSString *c = [str3 substringWithRange:range];

NSLog(@"c = %@",c);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


//5:字符串拼接

NSString *str5 = [[NSStringalloc]initWithFormat:@"lanlana"];

NSString *str6 = [str5 stringByAppendingString:@"pppp"];

NSLog(@"%@",str6);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//6.可变字符串

NSString *str7 = [[NSStringalloc]initWithString:@"sd sf dj kl pi"];

NSLog(@"%@",str7);

NSString *str8 = [str7 capitalizedString];//首字母大写,切记每个字母用空格分开

NSLog(@"%@",str8);

NSString *d = [[NSStringalloc]initWithString:@"MMGklL"];

NSString *str9 = [d lowercaseString];//小写变大写

NSLog(@"str9 = %@",str9);

NSString *str10= [d uppercaseString];//给定字符串中所有的字母都变成大写

NSLog(@"%@",str10);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//7.字符串比较

NSString *str11 = [[NSStringalloc]initWithFormat:@"lanlana"];


NSString *str12 = [[NSStringalloc]initWithFormat:@"lanlana"];


NSComparisonResult result = [str11 compare:str12];


NSLog(@"result = %ld",result);

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//8.  字符串数值


NSString *str13 = [[NSStringalloc]initWithFormat:@"10"];

int value = [str13 intValue];

NSLog(@"%d",value);

NSString *str14 = [[NSStringalloc]initWithFormat:@"3.8"];

float a1 = [str14 floatValue];

NSLog(@"%.2f",a1);

NSString *str15 = [[NSStringalloc]initWithFormat:@"4454.12233"];

double b1 = [str15 doubleValue];

NSLog(@"b1 = %f",b1);

NSString *str16 = [[NSStringalloc]initWithFormat:@"13.9"];

longlong c1 = [str16 longLongValue];

NSLog(@"c1 = %lld",c1);

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//E:字符串替换


NSString *str17 = [[NSStringalloc]initWithFormat:@"蓝欧科技有限公司"];

NSString *str18 = [str17 stringByReplacingOccurrencesOfString:@"蓝欧科技有限公司" withString:@"蓝欧3G学院"];

NSLog(@"str17 = %@",str17);

NSLog(@"str18 = %@",str18);



你可能感兴趣的:(字符串,NSString,追加)