c concatenate strings
To concatenate strings in Objective-C, you can use the stringByAppendingString:
method. This method appends a given string to the end of the receiver and returns a new string.
Here's an example of concatenating two strings in Objective-C:
NSString *string1 = @"Hello, ";
NSString *string2 = @"World!";
NSString *concatenatedString = [string1 stringByAppendingString:string2];
In this example, the stringByAppendingString:
method is used to append string2
to the end of string1
, resulting in Hello, World!
being stored in concatenatedString
.
You can also use the stringWithFormat:
method to concatenate strings with other values, like variables or constants. This method allows you to format your strings using placeholders.
Here's an example of concatenating a string with a variable using stringWithFormat:
:
NSString *name = @"John";
NSString *greeting = [NSString stringWithFormat:@"Hello, %@", name];
In this example, the %@
placeholder is used to indicate where the name
variable should be inserted in the formatted string.
These are the basic ways to concatenate strings in Objective-C using the stringByAppendingString:
and stringWithFormat:
methods.