How to concatenate all values of array in Xcode [duplicate] - iphone

This question already has answers here:
Join an Array in Objective-C
(3 answers)
Closed 9 years ago.
I am new in iOS and i am not able to find solution of my question . So please tell me how can i concatenate all values of array in iOS

Answer : -componentsJoinedByString: .
It will join the components in the array by the specified string and return a string representation of the array.
Use : NSString *finalStr = [yourArray componentsJoinedByString:#" "];
GoodLuck !!!

if you are using string values in NSArray then it is quite simple.
You can just run for loop and use a NSMutableString to append the data.
NSArray *_array;//Your Initialized Array
NSMutableString *_string =[[NSMutableString alloc]init];
for(int i= 0 ;i<[_array count];i++)
{
[_string appendFormat:#"%#",[_array objectAtIndex:i]];
}
NSLog(#"_string = %#",_string);

NSMutableString* theString = [NSMutableString string];
for (int i=0; i<[array count];i++){
[theString appendString:[NSString stringWithFormat:#"%i ",i]];
}
NSLog(#"mystirng=%#", theString;);

Related

how to copy the required Items of NSMutableArray to NSArray [duplicate]

This question already has answers here:
How to split an NSArray into two equal pieces?
(3 answers)
Closed 9 years ago.
I have NSMutableArray of Results it has 6 items. I want to copy the first three contents
to another NSArray.
Just use subarrayWithRange::
NSMutableArray *oldArray = ... // the mutable array with the 6 objects
NSArray *result = [oldArray subarrayWithRange:NSMakeRange(0, MIN(3, oldArray.count))];
NSMutalbeArray *newArray=[[NSMutableArray alloc] init];
for(int i=0; i < results.count; i++){
[newArray addObject:results[i]];
if(i == 2)
break;
}
Try this simple loop:
NSMutableArray *resultantArray=[NSMutableArray new];
for(NSInteger i=0;i<3;i++){
[resultantArray addObject:firstArray[i]];
}

How to get last array values from NsmutableArray [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the last object of an NSArray
In my iPhone application, I want to retrieve the last array value from NSMutableArray:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:messages];
NSLog(#"array : %#", array);
In this "array". I having this below values.
hi, hello,
I want to take hello values only from the NSMutableArray.
How can I do this?
Try this
NSLog(#"array : %#",[array lastObject]);
It may help's you
you should follow indexpath at [array count] - 1 to obtain the last value.
For Ex,
NSString *str = [NSString stringWithFormat:"%#",[array objectAtIndex:[array count] - 1]];
NSLog("last object of an array = %#",str);
Enjoy Programming!
You can use:
NSString *str = (NSString *)[array lastObject];
[array lastObject] returns the last object of the array.
NSArray *arr=[NSArray arrayWithObjects:#"A",#"B",#"C", nil];
NSString *last=[arr lastObject];
NSLog(#"Last obj in string : %#",last);
Output :
Last obj in string : C

NSMutableString append is not working [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective-C: How do you append a string to an NSMutableString?
I have a NSMutableArray. I want to extract all elements of this NSMutable array and append it in to a string. My following code is not working. String is not append to the variable. Also I am using automatic reference counting feature in this project.
NSMutableString *theString = [NSMutableString string];
for(int i=0; i<[self.incomingMessages count]; i++) {
id object = [self.incomingMessages objectAtIndex: i];
[theString appendString:(NSString*)object];
}
NSLog(#"the appended string: %#", theString);
[self.incomingMessages removeAllObjects];
How can I append the stored strings into the theString variable. Whats is the mistake I have done?
Do this
NSMutableString *theString = [[NSMutableString alloc]init ];
Try to print and see if your object is string
Maybe you need to initialize your theString like this?
NSMutableString *theString = [NSMutableString stringWithString:#""];

how to convert NSinteger to String [duplicate]

This question already has answers here:
How to convert int to NSString?
(4 answers)
Closed 8 years ago.
I want to convet int to string in objective c how to do that.
my code.
for (i=0; i<=200; i=i+10) {
// here i want to convet the value of i into string how to do this
}
Thanks in Advance.
Try this:
NSMutableString *myWord = [[NSMutableString alloc] init];
for (int i=0; i<=200; i=i+10) {
[myWord appendString:[NSString stringWithFormat:#"%d", i]];
//...
}
//do something with myWord...
[myWord release];
NSInteger is simply a typedef to the int or long data types on 32/64-bit systems.
NSInteger n = 13;
NSString string = #(n).stringValue;
Reference see Objective-C literals - literals remove lots of ugly boilerplate code cluttering up your codebase: http://clang.llvm.org/docs/ObjectiveCLiterals.html
You may want to declare myWord out of the loop as NSMutableString.

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

This question already has answers here:
Closed 11 years ago.
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 :(
Try this:
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
[theString appendString:[NSString stringWithFormat:#"%i ",i]];
}
label.text = theString;
Since you're using a loop, do be somewhat careful with both Tom and Benjie's solutions. They each create an extra autoreleased object per iteration. For a small loop, that's fine, but if the size of the loop is unbounded or if the strings are large, this can lead to a very large memory spike and performance hit. Particularly on iPhone, this is exactly the kind of loop that can lead to surprising memory problems due to short-lived memory spikes.
The following solution has a smaller memory footprint (it's also slightly faster and takes less typing). Note the call to -appendFormat: rather than -appendString. This avoids creating a second string that will be thrown away. Remember that the final string has an extra space at the end that you may want to get rid of. You can fix that by either treating the first or last iteration differently, or by trimming the last space after the loop.
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
[theString appendFormat:#"%i ",i];
}
label.text = theString;
Don't forget [NSArray componentsJoinedByString:]. In this case you don't have an NSArray, but in the common cases where you do, this is probably the best way to get what you're looking for.
//NSArray *chunks
string = [chunks componentsJoinedByString: #","];
Another method without using NSMutableString:
NSString* theString = #"";
for (int i=0; i<=10;i++){
theString = [theString stringByAppendingFormat:#"%i ",i];
}
label.text = theString;
Here's a full implementation (correcting your ranges):
-(IBAction) updateText: (id)sender {
int a[3];
a[0]=1;
a[1]=2;
a[2]=3;
NSString *str = #"";
for (int i=0; i<3;i++)
str = [str stringByAppendingFormat:#"%i ",i];
label.text = str;
}
You could also do it like this (e.g. if you wanted a comma separated list):
-(IBAction) updateText: (id)sender {
int a[3];
a[0]=1;
a[1]=2;
a[2]=3;
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:3];
for (int i=0; i<3;i++)
[arr addObject:[NSString stringWithFormat:#"%i",i]];
label.text = [arr componentsJoinedByString:#", "];
}