Convert NSArray to NSString in Objective-C
Solution 1
NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
Solution 2
One approach would be to iterate over the array, calling the description
message on each item:
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
[result appendString:[obj description]];
}
NSLog(@"The concatenated string is %@", result);
Another approach would be to do something based on each item's class:
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
if ([obj isKindOfClass:[NSNumber class]])
{
// append something
}
else
{
[result appendString:[obj description]];
}
}
NSLog(@"The concatenated string is %@", result);
If you want commas and other extraneous information, you can just do:
NSString * result = [array description];
Solution 3
I think Sanjay's answer was almost there but i used it this way
NSArray *myArray = [[NSArray alloc] initWithObjects:@"Hello",@"World", nil];
NSString *greeting = [myArray componentsJoinedByString:@" "];
NSLog(@"%@",greeting);
Output :
2015-01-25 08:47:14.830 StringTest[11639:394302] Hello World
As Sanjay had hinted - I used method componentsJoinedByString from NSArray that does joining and gives you back NSString
BTW NSString has reverse method componentsSeparatedByString that does the splitting and gives you NSArray back .
Solution 4
I recently found a really good tutorial on Objective-C Strings:
http://ios-blog.co.uk/tutorials/objective-c-strings-a-guide-for-beginners/
And I thought that this might be of interest:
If you want to split the string into an array use a method called componentsSeparatedByString to achieve this:
NSString *yourString = @"This is a test string";
NSArray *yourWords = [myString componentsSeparatedByString:@" "];
// yourWords is now: [@"This", @"is", @"a", @"test", @"string"]
if you need to split on a set of several different characters, use NSString’s componentsSeparatedByCharactersInSet:
NSString *yourString = @"Foo-bar/iOS-Blog";
NSArray *yourWords = [myString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"-/"]
];
// yourWords is now: [@"Foo", @"bar", @"iOS", @"Blog"]
Note however that the separator string can’t be blank. If you need to separate a string into its individual characters, just loop through the length of the string and convert each char into a new string:
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];
for (int i=0; i < [myString length]; i++) {
NSString *ichar = [NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];
[characters addObject:ichar];
}
Solution 5
NSString * str = [componentsJoinedByString:@""];
and you have dic or multiple array then used bellow
NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
Related videos on Youtube
Comments
-
alexyorke almost 4 years
I am wondering how to convert an NSArray
[@"Apple", @"Pear ", 323, @"Orange"]
to a string in Objective-C.-
beryllium about 12 years
-
alexyorke over 11 years@beryllium That question was asked two years after my question.
-
-
Stefan over 14 yearsIf the array has many elements it might be more efficient to first convert all elements to strings (probably using
-description
) and concat them after that using-componentsJoinedByString:
with@""
as the parameter. -
TechZen over 14 yearsI would go with this method over the one by Dave Delong unless your just debugging.
-
TechZen over 14 yearsWon't this accomplish the same thing as calling [array description]?
-
Dave DeLong over 14 years@TechZen - no, because
[array description]
inserts newlines and the outer parentheses. -
Deamon almost 13 yearsThis should be the chosen answer!
-
zumzum over 11 yearsperfect solution. Exactly what I needed.
-
Brigham over 11 yearsAwesome. The name valueForKey does not make its purpose obvious.
-
Mick MacCallum over 11 yearsJust want to be sure here, but is
NSString * myString = [array componentsJoinedByString:@""];
an acceptable substitute for this? -
jweyrich about 11 years@0x7fffffff: It's equivalent if the array contains only "basic" types. For complex types, it will stringify them as
<ClassName: InstanceAddress>
. ThevalueForKey
makes it retrieve the specified property for each item. In this case,description
is aNSString *
property fromNSObject
, whose getter can be overriden by its subclasses. -
Donald Duck over 6 yearsWhile this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
-
AbdulAziz Rustam Ogli over 6 yearsThank you for your advice, sincerely!
-
silverdr over 3 yearsThis is in no way answering the question, is it?
-
silverdr over 3 yearsThe first section (Objective-C) does not add any value to the almost decade old accepted answer. The other sections do not answer the question, which is explicitly about Objective-C.