How to add space between two related NSStrings?

I have three string objects:

NSString *firstName;
NSString *lastName;
NSString *fullName;

Values ​​for firstNameand lastNameare taken from NSTextFields.

Then I want to combine the two lines and put the result in fullname.

This is the code I'm using:

fullName = [firstName stringByAppendingString:lastName];

However, the result does not put a space between the two names (e.g. JohnSmith).

How to add in the sample? I would like the result to look like (John Smith).

+3
source share
4 answers

The easiest way:

fullname = [[firstName stringByAppendingString:@" "] stringByAppendingString:lastName];

i.e. add a space and then add lastName.

+8
source

I am struck by the length of the remaining answers:

fullname = [NSString stringWithFormat:@"%@ %@", firstname, lastname];
+14
source
fullName = [@[firstName, lastName]
             componentsJoinedByString:@" "];
+3

. .

(NSString*) description { return [[[NSString alloc] 
                          initWithFormat:" %@ %@", 
                          firstname, 
                          lastname] autorelease]; }
0

Source: https://habr.com/ru/post/1705156/


All Articles