我正在尝试连接一些 NSString,但想排除那些为 null 的字符串。我正在使用这个解决方案:
[NSString stringWithFormat:@"%@/%@/%@", three, two, one];
但是如果其中一个字符串为空怎么办?我想排除它。有任何想法吗?
谢谢。
最佳答案
你可以这样做:
[NSString stringWithFormat:@"%@/%@/%@", three ?: @"", two ?: @"", one ?: @""];
或者更好的可能是拥有一个可变字符串并构建它:
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:0];
if (three) {
[string appendFormat:@"%@/", three];
}
if (two) {
[string appendFormat:@"%@/", two];
}
if (one) {
[string appendFormat:@"%@/", one];
}
关于iphone - 连接 nsstring 并排除空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10666322/