189

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

alexyorke
  • 4,224
  • 3
  • 34
  • 55

9 Answers9

544
NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
alexyorke
  • 4,224
  • 3
  • 34
  • 55
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 1
    Won't this accomplish the same thing as calling [array description]? – TechZen Dec 01 '09 at 20:55
  • 11
    @TechZen - no, because `[array description]` inserts newlines and the outer parentheses. – Dave DeLong Dec 01 '09 at 21:05
  • This should be the chosen answer! – pixelfreak Jul 12 '11 at 01:05
  • Awesome. The name valueForKey does not make its purpose obvious. – Brigham Nov 08 '12 at 21:18
  • 57
    Just want to be sure here, but is `NSString * myString = [array componentsJoinedByString:@""];` an acceptable substitute for this? – Mick MacCallum Nov 27 '12 at 17:48
  • 12
    @0x7fffffff: It's equivalent if the array contains only "basic" types. For complex types, it will stringify them as ``. 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. – jweyrich Apr 25 '13 at 01:19
32

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];
Jason
  • 28,040
  • 10
  • 64
  • 64
  • 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. – Georg Schölly Dec 01 '09 at 20:34
  • I would go with this method over the one by Dave Delong unless your just debugging. – TechZen Dec 01 '09 at 20:53
18

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 .

Pranay
  • 181
  • 1
  • 2
10

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];
}
MarkP
  • 2,546
  • 5
  • 31
  • 48
9
NSString * str = [componentsJoinedByString:@""];

and you have dic or multiple array then used bellow

NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];   
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Sanjay Kakadiya
  • 107
  • 1
  • 7
4
NSArray *array = [NSArray arrayWithObjects:@"One",@"Two",@"Three", nil];
NSString *stringFromArray = [array componentsJoinedByString:@" "];

The first line initializes an array with objects. The second line joins all elements of that array by adding the string inside the "" and returns a string.

2

Objective C Solution

NSArray * array = @[@"1", @"2", @"3"];
NSString * stringFromArray = [[array valueForKey:@"description"] componentsJoinedByString:@"-"];   // "1-2-3"

Those who are looking for a solution in Swift

If the array contains strings, you can use the String's join method:

var array = ["1", "2", "3"]

let stringFromArray = "-".join(array) // "1-2-3"

In Swift 2:

var array = ["1", "2", "3"]

let stringFromArray = array.joinWithSeparator("-") // "1-2-3"

In Swift 3 & 4

var array = ["1", "2", "3"]

let stringFromArray = array.joined(separator: "-") // "1-2-3"
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
  • 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. – silverdr Jan 12 '21 at 11:30
-2

The way I know is easy.

var NSArray_variable = NSArray_Object[n]
var stringVarible = NSArray_variable as String

n is the inner position in the array This in SWIFT Language. It might work in Objective C

erhun
  • 3,549
  • 2
  • 35
  • 44
jasmo2
  • 525
  • 7
  • 13
-2

Swift 3.0 solution:

let string = array.joined(separator: " ")
AhmedZah
  • 1,203
  • 2
  • 9
  • 9