How to concatenate a string in Objective-C
I’m writing this article with the objective of hopefully helping you if you’re looking how to concatenate a string but also to vent just a tiny little bit.
Firstly, to set the scene. You have two strings and you want to join them together. For example, string1 is something like “Fly” and string2 is something else like “Digital” and you want to join them as string3 to become something new such as “Fly Digital”.
In ASP Classic, this is a breeze:
<% string1="Fly" string2="Digital" string3=string1 & " " & string2 Response.Write(string3) %>
In PHP, not a problem:
<?php $string1="Fly"; $string2="Digital"; $string3 = $string1 . " " . $string2; echo $string3; ?>
But Objective-C doesn’t quite use the same logic of string1 + string2 = string3. Instead you need to call a whole new method called stringByAppendingString, which is where I start to ask questions. But first, here’s how I do it in Objective-C:
NSString *string1 = @"Fly"; NSString *string2 = @"Digital"; NSString *string3; string3 = [string1 stringByAppendingString:string2];