Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate two strings on iPhone?

Tags:

string

iphone

How to connect string "Hello" and string "World" to "HelloWorld"? Looks like "+" doesn't work.

like image 929
Chilly Zhong Avatar asked Feb 26 '09 06:02

Chilly Zhong


People also ask

How do you concatenate 2 strings?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

How do I type concatenation symbol?

You can use the concatenation operator ( || ) to concatenate two expressions that evaluate to character data types or to numeric data types.


2 Answers

NSString *string = [NSString stringWithFormat:@"%@%@", @"Hello", @"World"];
NSLog(@"%@", string);

That should do the trick, although I am sure there is a better way to do this, just out of memory. I also must say this is untested so forgive me. Best thing is to find the stringWithFormat documentation for NSString.

like image 80
Garrett Avatar answered Sep 22 '22 02:09

Garrett


How about:

NSString *hello = @"Hello";
NSString *world = @"World";
NSString *helloWorld = [hello stringByAppendingString:world];
like image 30
Christopher Avatar answered Sep 24 '22 02:09

Christopher