The output of the [Character] Array currently is:
[字符]数组当前的输出为:
["E", "x", "a", "m", "p", "l", "e"]
It should be:
应该是:
Example
It could be that "
is in the array, like this: """
. That output should be "
.
它可以是“在数组中,像这样:”。这个输出应该是。
Thank you!
谢谢你!
3 个解决方案
#1
17
The other answer(s) cover the case where your array is one of String
elements (which is most likely the case here: since you haven't supplied us the type of the array, we could use Swift own type inference rules to speculatively infer the type to be [String]
).
另一个答案涉及到数组属于字符串元素的情况(这里的情况很可能是这样:因为您没有提供数组的类型,所以我们可以使用Swift自己的类型推断规则推测类型为[String])。
In case the elements of your array are in fact of type Character
, however, you could use the Character
sequence initializer of String
directly:
如果数组中的元素实际上是字符类型,那么可以直接使用字符串的字符序列初始化器:
let charArr: [Character] = ["E", "x", "a", "m", "p", "l", "e"]
let str = String(charArr) // Example
W.r.t. your comment below: if your example array is, for some reason, one of Any
elements (which is generally not a good idea to use explicitly, but sometimes the case when recieving data from some external source), you first need to perform an attempted conversion of each Any
element to String
type, prior to concenating the converted elements to a single String
instance. After the conversion, you will be working with an array of String
elements, in which case the methods shown in the other answers will be the appropriate method of concenation:
数组W.r.t.下面你的评论:如果你的例子是,由于某种原因,任何元素之一(这通常不是一个好主意使用显式,但有时收到数据时,从外部源),您首先需要执行一个尝试每个任何元素转换成字符串类型,之前concenating元素转换成一个字符串实例。转换后,您将使用一系列字符串元素,在这种情况下,其他答案中显示的方法将是适当的重音方法:
// e.g. using joined()
let arr: [Any] = ["E", "x", "a", "m", "p", "l", "e"]
let str = arr.flatMap { $0 as? String }.joined()
print(str) // example
You could naturally also (attempt to) convert from Any
to Character
elements, but even in this case you would have to go via String
instances, which means that for the [Any]
case, the joined()
alternative above is to prefer over the one below:
当然,您也可以(尝试)从任何字符元素转换为字符元素,但即使在这种情况下,您也必须通过字符串实例,这意味着对于[Any]情况,上面的join()选项是选择下面的选项:
let arr: [Any] = ["E", "x", "a", "m", "p", "l", "e"]
let str = String(arr.flatMap { ($0 as? String)?.characters.first })
print(str) // example
#2
6
Just use joined()
with default ""
separator:
只需要使用默认“”分隔符:
let joinedString = ["E", "x", "a", "m", "p", "l", "e"].joined()
#3
3
let e = ["E", "x", "a", "m", "p", "l", "e"]
print(e.reduce ("", +))
#1
17
The other answer(s) cover the case where your array is one of String
elements (which is most likely the case here: since you haven't supplied us the type of the array, we could use Swift own type inference rules to speculatively infer the type to be [String]
).
另一个答案涉及到数组属于字符串元素的情况(这里的情况很可能是这样:因为您没有提供数组的类型,所以我们可以使用Swift自己的类型推断规则推测类型为[String])。
In case the elements of your array are in fact of type Character
, however, you could use the Character
sequence initializer of String
directly:
如果数组中的元素实际上是字符类型,那么可以直接使用字符串的字符序列初始化器:
let charArr: [Character] = ["E", "x", "a", "m", "p", "l", "e"]
let str = String(charArr) // Example
W.r.t. your comment below: if your example array is, for some reason, one of Any
elements (which is generally not a good idea to use explicitly, but sometimes the case when recieving data from some external source), you first need to perform an attempted conversion of each Any
element to String
type, prior to concenating the converted elements to a single String
instance. After the conversion, you will be working with an array of String
elements, in which case the methods shown in the other answers will be the appropriate method of concenation:
数组W.r.t.下面你的评论:如果你的例子是,由于某种原因,任何元素之一(这通常不是一个好主意使用显式,但有时收到数据时,从外部源),您首先需要执行一个尝试每个任何元素转换成字符串类型,之前concenating元素转换成一个字符串实例。转换后,您将使用一系列字符串元素,在这种情况下,其他答案中显示的方法将是适当的重音方法:
// e.g. using joined()
let arr: [Any] = ["E", "x", "a", "m", "p", "l", "e"]
let str = arr.flatMap { $0 as? String }.joined()
print(str) // example
You could naturally also (attempt to) convert from Any
to Character
elements, but even in this case you would have to go via String
instances, which means that for the [Any]
case, the joined()
alternative above is to prefer over the one below:
当然,您也可以(尝试)从任何字符元素转换为字符元素,但即使在这种情况下,您也必须通过字符串实例,这意味着对于[Any]情况,上面的join()选项是选择下面的选项:
let arr: [Any] = ["E", "x", "a", "m", "p", "l", "e"]
let str = String(arr.flatMap { ($0 as? String)?.characters.first })
print(str) // example
#2
6
Just use joined()
with default ""
separator:
只需要使用默认“”分隔符:
let joinedString = ["E", "x", "a", "m", "p", "l", "e"].joined()
#3
3
let e = ["E", "x", "a", "m", "p", "l", "e"]
print(e.reduce ("", +))