Convert NSArray to NSString in Objective-C

192,433

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:@""];   
Share:
192,433

Related videos on Youtube

alexyorke
Author by

alexyorke

(your about me is currently blank)

Updated on June 09, 2020

Comments

  • alexyorke
    alexyorke almost 4 years

    I am wondering how to convert an NSArray [@"Apple", @"Pear ", 323, @"Orange"] to a string in Objective-C.

  • Stefan
    Stefan over 14 years
    If 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
    TechZen over 14 years
    I would go with this method over the one by Dave Delong unless your just debugging.
  • TechZen
    TechZen over 14 years
    Won't this accomplish the same thing as calling [array description]?
  • Dave DeLong
    Dave DeLong over 14 years
    @TechZen - no, because [array description] inserts newlines and the outer parentheses.
  • Deamon
    Deamon almost 13 years
    This should be the chosen answer!
  • zumzum
    zumzum over 11 years
    perfect solution. Exactly what I needed.
  • Brigham
    Brigham over 11 years
    Awesome. The name valueForKey does not make its purpose obvious.
  • Mick MacCallum
    Mick MacCallum over 11 years
    Just want to be sure here, but is NSString * myString = [array componentsJoinedByString:@""]; an acceptable substitute for this?
  • jweyrich
    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>. The valueForKey makes it retrieve the specified property for each item. In this case, description is a NSString * property from NSObject, whose getter can be overriden by its subclasses.
  • Donald Duck
    Donald Duck over 6 years
    While 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
    AbdulAziz Rustam Ogli over 6 years
    Thank you for your advice, sincerely!
  • silverdr
    silverdr over 3 years
    This is in no way answering the question, is it?
  • silverdr
    silverdr over 3 years
    The 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.