Objective C String concatenation example

Well in any programming language this is considered one of important string manipulation functions and as simple as adding a plus sign between the two strings. Guess what its not that easy in Objective C, here is how to achieve string concatenation when using either NSString or NSMutableString classes.
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])

    @autoreleasepool {
        NSString *sql_stmt = @"CREATE TABLE IF NOT EXISTS EMPLOYEES (";
        sql_stmt = [sql_stmt stringByAppendingString:@"id INTEGER PRIMARY KEY AUTOINCREMENT, "];
        sql_stmt = [sql_stmt stringByAppendingString:@"name TEXT, "];
        sql_stmt = [sql_stmt stringByAppendingString:@"department TEXT, "];
        sql_stmt = [sql_stmt stringByAppendingString:@"age TEXT)"];

        NSMutableString *sql_stmt2 = [[NSMutableString alloc] initWithString:@"CREATE TABLE IF NOT EXISTS EMPLOYEES ("];
        [sql_stmt2 appendString:@"id INTEGER PRIMARY KEY AUTOINCREMENT, "];
        [sql_stmt2 appendString:@"name TEXT, "];
        [sql_stmt2 appendString:@"department TEXT, "];
        [sql_stmt2 appendString:@"age TEXT)"];
    return 0;

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.