iOS macOS拼接字符串
1.stringWithFormat
NSString *str1 = @"666"; NSString *str2 = @"888"; NSString *strAll = [NSString stringWithFormat:@"%@%@",str1, str2];
NSLog(@"输出结果:%@",strAll);
输出结果:666888
2.initWithFormat
NSString *str1 = @"555"; NSString *str2 = @"777"; NSString *strAll = [[NSString alloc]initWithFormat:@"%@%@",str1,str2];
NSLog(@"输出结果:%@",strAll);
输出结果:555777
3.stringByAppendingString
NSString *str1 = @"333"; NSString *str2 = @"444"; NSString *strAll = [str1 stringByAppendingString:str2];
NSLog(@"输出结果:%@",strAll);
输出结果:333444
4.可变字符串NSMutableString
NSString *str1 = @"555"; NSString *str2 = @"777"; NSMutableString *strAll = [NSMutableString string]; [strAll appendString:str1]; [strAll appendString:str2];
NSLog(@"输出结果:%@",strAll);
输出结果:555777
5.stringByAppendingFormat
NSString *str1 = @"888"; NSString *str2 = @"999"; NSString *strAll = [[NSString alloc]init];//或者这样初始化 NSString *strAll = [NSString string];
strAll = [strAll stringByAppendingFormat:@"%@%@",str1,str2];
NSLog(@"输出结果:%@",strAll);
输出结果:888999