在与stringByAppendingString连接时注入一个字符

时间:2013-11-15 11:18:35

标签: objective-c concatenation code-injection

有没有办法连接两个字符串并注入一个字符。我有:

NSString *firstString = @"http://www.stackoverflow.com";
NSString *secondString = @"supercooloptions";
NSString *result = [firstString stringByAppendingString: secondString];
NSLog(@"the final string with "/" injection = %@", result);

我想要注入“/”的最终字符串= www.stackoverflow.com/supercooloptions

2 个答案:

答案 0 :(得分:6)

您可以使用其他方法 - 例如stringWithFormat

NSString *result = [NSString stringWithFormat:@"%@/%@", firstString, secondString];

答案 1 :(得分:5)

对于此特定情况,请使用stringByAppendingPathComponent:

NSString *result = [firstString stringByAppendingPathComponent:secondString];