如何在 iOS Swift 中获取数组中所有字符串的第一个字母?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33792991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:15:59  来源:igfitidea点击:

How to get first letter of all strings in an array in iOS Swift?

iosswiftnsarray

提问by AAA

I have a group of strings stored in an array.

我有一组存储在数组中的字符串。

stringArray = [Aruna, Bala, Chitra, Divya, Fatima]

I want to get the first letters of all the strings and store it in an array. Like: letterArray = [A, B, C, D, F]

我想获取所有字符串的第一个字母并将其存储在一个数组中。喜欢:letterArray = [A, B, C, D, F]

Note: They are not within quotes "--"

注意:它们不在引号“--”内

回答by myles

Not sure what you mean by 'they are not within quotes', but if they are actually Strings then something like this:

不确定“它们不在引号内”是什么意思,但如果它们实际上是字符串,则如下所示:

var letterArray = [Character]()
for string in stringArray {
    letterArray.append(string.characters.first!)
}

EDIT

编辑

To have it as Stringinstead as you wish:

有它String,而不是如你所愿:

var letterArray = [String]()
for string in stringArray {
    letterArray.append(String(string.characters.first!))
}

EDIT 2

编辑 2

As Leo Dabussuggests, if you pass an empty string the above will fail. If you know there will never be an empty string this doesn't apply, but I've updated the above to handle this case:

正如Leo Dabus 所建议的,如果您传递一个空字符串,则上述内容将失败。如果你知道永远不会有空字符串,这不适用,但我已经更新了上面的内容来处理这种情况:

var letterArray = [String]()
for string in stringArray {
    if let letter = string.characters.first {
        letterArray.append(String(letter))
    }
}

UPDATE: SWIFT 4

更新:SWIFT 4

From Swift 4 charactershas been deprecated. Instead of using string.characters.firstyou should now operate on the String directly using just string.first. For example:

从 Swift 4 开始characters已被弃用。而不是使用string.characters.first你现在应该直接只使用字符串操作string.first。例如:

var letterArray = [String]()
for string in stringArray {
    if let letter = string.first {
        letterArray.append(String(letter))
    }
}

回答by Leo Dabus

Xcode 9 ? Swift 4

Xcode 9 ? 斯威夫特 4

extension Collection where Element: StringProtocol {
    var initials: [Element.SubSequence] {
        return map { 
extension Collection where Iterator.Element == String {
    var initials: [String] {
        return map{String(
extension CollectionType where Generator.Element == String {
    var initials: [String] {
        return map{ String(
let stringArray = ["Aruna", "Bala", "Chitra", "Divya", "Fatima"]

let result = stringArray.map({ ##代码##.prefix(1) }) // ["A", "B", "C", "D", "F"]
.characters.prefix(1)) } } } let stringArray = ["Aruna", "Bala", "Chitra", "Divya", "Fatima"] let initials = stringArray.initials // ["A", "B", "C", "D", "F"]
.characters.prefix(1))} } }
.prefix(1) } } }


Xcode 8 ? Swift 3

Xcode 8 ? 斯威夫特 3

##代码##

Xcode 7.3.1 ? Swift 2.2.1

Xcode 7.3.1?斯威夫特 2.2.1

##代码##

回答by Vladyslav Matsko

Probably you should try my solution.

也许你应该试试我的解决方案。

##代码##