Join an Array in Objective-C

后端 未结 3 1131
逝去的感伤
逝去的感伤 2020-12-12 20:18

I\'m looking for a method of turning a NSMutableArray into a string. Is there anything on a par with this Ruby array method?

>> array1 = [1, 2, 3]
>         


        
相关标签:
3条回答
  • 2020-12-12 20:36
    NSArray  *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
    NSString *joinedString = [array1 componentsJoinedByString:@","];
    

    componentsJoinedByString: will join the components in the array by the specified string and return a string representation of the array.

    0 讨论(0)
  • 2020-12-12 20:45

    The method you are looking for is componentsJoinedByString.

    NSArray  *a = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];//returns a pointer to NSArray
    NSString *b = [a componentsJoinedByString:@","];//returns a pointer to NSString
    NSLog(@"%@", b); // Will output 1,2,3
    
    0 讨论(0)
  • 2020-12-12 20:47

    NSArray class reference:

    NSArray *pathArray = [NSArray arrayWithObjects:@"here",
        @"be", @"dragons", nil];
    NSLog(@"%@",
        [pathArray componentsJoinedByString:@" "]);
    
    0 讨论(0)
提交回复
热议问题