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 :(
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.
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.
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.
Try this:
NSMutableString* theString = [NSMutableString string]; for (int i=0; i<=10;i++){ [theString appendString:[NSString stringWithFormat:@"%i ",i]]; } label.text = theString;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With