Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Concatenate String in Objective-C (iPhone)? [duplicate]

Tags:

Possible Duplicate:
How do I concatenate strings in Objective-C?

Firstly, the platform is iPhone and label.text changes the label displayed. Consider this scenario:

I've an array of integers. And I want to display it on the screen.

Here's my take on it:

-(IBAction) updateText: (id)sender {    int a[2];    a[0]=1;    a[1]=2;    a[2]=3;    for (int i=0; i<=10;i++)      label.text = [NSString stringByAppendingString: [NSString stringWithFormat: @"%i", a[i]]];  } 

As you can probably see, I'm pretty confused. Pls pls help me out :(

like image 361
r0ach Avatar asked Jul 21 '09 12:07

r0ach


People also ask

How do you concatenate in Objective C?

You use it with @"%@%@" to concatenate two strings, @"%@%@%@" to concatenate three strings, but you can put any extra characters inside, print numbers, reorder parameters if you like and so on. The format string can be localised, making it ten times more powerful. String concatenation is for beginners.

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.

Can you use a double in string concatenation?

The “+” operator with a String acts as a concatenation operator. Whenever you add a String value to a double using the “+” operator, both values are concatenated resulting a String object. In-fact adding a double value to String is the easiest way to convert a double value to Strings.


1 Answers

Try this:

NSMutableString* theString = [NSMutableString string]; for (int i=0; i<=10;i++){     [theString appendString:[NSString stringWithFormat:@"%i ",i]]; } label.text = theString; 
like image 153
Tom Dalling Avatar answered Nov 24 '22 08:11

Tom Dalling