1

Hi I am trying to concatenate some string variables that may be nil. Specifically, I am taking the title, first name and last name of a contact and want to assemble the best possible name i.e. Dr. John Smith without getting any nulls in the result.

If I simply do:

NSString *title = contact.title;
NSString *first = contact.first;
NSString *last = contact.last;
NSString *bestName = [NSString stringWithFormat: @"%@ %@ %@", title, first,last];

...if any of them are null I get a (null) in the result.

Can anyone suggest efficient code to only include if the value is not null?

Thanks for any suggestions.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Do you really want an extra leading space if `title` is nil, or an internal double-space if `first` is nil? Or do you really mean to join with spaces, ignoring nil elements? – Rob Napier Jul 08 '15 at 01:12

3 Answers3

4

For each of your string components, add it to an NSMutableArray only if it's non-nil. Then use [array componentsJoinedByString:" "] on the array.

Objective C - How to concatenate an entire array of strings?

Alternatively, you could do the following, which will still leave additional whitespace and might not be desirable. For each of your strings you're concatenating:

NSString *prop = obj.prop == nil ? @"" : obj.prop
turingtested
  • 6,356
  • 7
  • 32
  • 47
Rikki Gibson
  • 4,136
  • 23
  • 34
  • `"" ` should be `@""`, but also as I state in my answer, this entire expression can be written as `NSString *prop = obj.prop?:@"";` – Lyndsey Scott Jul 08 '15 at 01:08
  • I like the array componentsJoinedByString which in this case could be an empty space. But how do you handle the if not nil logic? –  Jul 08 '15 at 01:12
  • if (obj.prop != nil) array.addObject(obj.prop); – Rikki Gibson Jul 08 '15 at 01:17
1

Although rikkigibson's answer is correct, the syntax can be simplified to

NSString *prop = obj.prop?:@"";

So in this case, you can format your string like so:

NSString *bestName = [NSString stringWithFormat: @"%@ %@ %@", title?:@"", first?:@"",last?:@""];

to print empty strings in place of "(null)".

Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
0

Just want to add to Rikki Gibson's answer, that sometimes it's useful is to take advantage of the nil-terminate init method in NSArray for those situations, like this:

NSString *bestName = [[NSArray arrayWithObjects:contact.title, contact.first, contact.last, nil] componentsJoinedByString:@" "];

Then it doesn't matter if any object is nil, it will only terminate the list. So you will be sure that that 1) it won't crash, and 2) no extra space will be added. The obvious disadvantage is however that if the title is nil, the first and last will not be appended... but if you can live with that, this method is very robust and simple.

turingtested
  • 6,356
  • 7
  • 32
  • 47
  • @Cristik, please define "won't work", because `NSLog(@"%@", [@[] componentsJoinedByString:@" "]);` works. The only problem is that (as I mentioned in the answer) it will be an empty string if the first object is nil. – turingtested Feb 04 '19 at 13:07
  • Sorry, I missed the disclaimer in the answer regarding what happens if `title` is nil. Looks like you covered this scenario. – Cristik Feb 04 '19 at 13:43