拼接字符串
//拼接字符串
NSString* string1, string2; //已存在的字符串,需要将string1和string2连接起来
//方法1.string = [NSString initWithFormat:@"%@,%@", string1, string2 ];
//方法2.string = [string1 stringByAppendingString:string2];
//方法3 .string = [string stringByAppendingFormat:@"%@,%@",string1, string2];
截取字符串
//截取字符串
NSString*string =@"sdfsfsfsAdfsdf";
string = [string substringToIndex:7];//截取下标7之前的字符串NSLog(@"截取的值为:%@",string);
[string substringFromIndex:2];//截取下标2之后的字符串NSLog(@"截取的值为:%@",string);
匹配字符串
//匹配字符串
NSString*string =@"sdfsfsfsAdfsdf";NSRangerange = [stringrangeOfString:@"f"];//匹配得到的下标NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范围类的字符串NSLog(@"截取的值为:%@",string);
分隔字符串
//分隔字符串
NSString*string =@"sdfsfsfsAdfsdf";
NSArray *array = [string componentsSeparatedByString:@"A"];
//从字符A中分隔成2个元素的数组NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf