目标C -如何连接整个字符串数组?

时间:2022-01-21 19:18:14

I am an Objective C newbie. I want to write a method that takes in an array of strings and returns a concatenated string, with a comma (,) in between each string. So if an array is {a b c d}, I want to return a,b,c,d.

我是一个客观的菜鸟。我想编写一个方法,它接收一个字符串数组并返回一个连接的字符串,每个字符串之间都有一个逗号(,)。如果一个数组是{a b c d},我想返回a b c d。

What is the easiest way to do that?

最简单的方法是什么?

2 个解决方案

#1


125  

There are many ways to do it, the simplest being

有很多方法可以做到,最简单的方法。

[yourArray componentsJoinedByString: @","]

#2


17  

Use NSArray's componentsJoinedByString: method.

使用NSArray componentsJoinedByString:方法。

NSArray *strings = ...;
NSString *combined = [strings componentsJoinedByString:@","];

#1


125  

There are many ways to do it, the simplest being

有很多方法可以做到,最简单的方法。

[yourArray componentsJoinedByString: @","]

#2


17  

Use NSArray's componentsJoinedByString: method.

使用NSArray componentsJoinedByString:方法。

NSArray *strings = ...;
NSString *combined = [strings componentsJoinedByString:@","];