Created
January 18, 2013 21:12
-
-
Save shineycode/4568567 to your computer and use it in GitHub Desktop.
ways to concatenate strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Different ways to concatenate strings | |
// | |
// Method 1: Using stringByAppendingString | |
// | |
NSString *string1 = @"First Text "; | |
NSString *string2 = @"Joined With Second"; | |
NSString *stringAppendResult = [string1 stringByAppendingString:string2]; | |
// Result: First Text Joined With Second | |
NSLog(@"stringAppendResult: %@", stringAppendResult); | |
// | |
// Method 2: Using literals (fancy new technique in the latest version of Objective-C) | |
// | |
NSString *concatWithLiterals = @"This " @"shows " @"how you would " @"concatenate a bunch of strings!"; | |
// Result: This shows how you would concatenate a bunch of strings! | |
NSLog(@"with literals: %@", concatWithLiterals); | |
// | |
// Method 3: If you're working with NSMutableString (NSString contents cannot be changed once you set it) | |
// | |
NSMutableString *mutableString = [NSMutableString stringWithString:@"Hello there, "]; | |
NSString *regularString = @"this is an third example."; | |
// Contents of mutableString1 is modified but string2 remains unchanged | |
[mutableString appendString:regularString]; | |
// Result: Hello there, this is an third example. | |
NSLog(@"with NSMutableString: %@", mutableString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment