Tutorial :Concatenating NSStrings in Objective C



Question:

How do I concatenate to NSStrings together in Objective C?


Solution:1

If you have a mutable string then you can do:

NSMutableString* someString = [NSMutableString stringWithString: @"Hello"];  [someString appendString: @", world!"];  

For example. Be more specific if this is not the answer you are looking for.


Solution:2

If the string is not mutable, you will instead want:

NSString *firstString = @"FirstString";  NSString *secondString = @"SecondString";  NSString *concatinatedString = [firstString stringByAppendingString:secondString];  // Note that concatinatedString is autoreleased,   // so if you may want to [concaticatedString retain] it.  

For completeness, here's the answer for a mutable string:

NSMutableString *firstString = [NSMutableString stringWithString:@"FirstString"];  NSString *secondString = @"SecondString";  [firstString appendString:secondString];  // Note that firstString is autoreleased,   // so if you may want to [firstString retain] it.  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »