I am trying to concatenate a few NSStrings but would like to exclude the ones that are nulls. I am using this solution:
[NSString stringWithFormat:@"%@/%@/%@", three, two, one];
but what if one of the string is null? I'd like to exclude it. any ideas?
thanks.
You could do:
[NSString stringWithFormat:@"%@/%@/%@", three ?: @"", two ?: @"", one ?: @""];
Or better would probably be to have a mutable string and build it up:
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:0];
if (three) {
[string appendFormat:@"%@/", three];
}
if (two) {
[string appendFormat:@"%@/", two];
}
if (one) {
[string appendFormat:@"%@/", one];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With