我怎样才能把一个字符串的每一个单词到斯威夫特的阵列? [英] How can I put each word of a string into an array in Swift?
本文介绍了我怎样才能把一个字符串的每一个单词到斯威夫特的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能将一个字符串的每个字成斯威夫特一个数组?
例如:
无功海峡=你好,游乐场!
到
VAR ARR = [你好,游乐场]
感谢您:)
解决方案
扩展字符串{
VAR单词表:[字符串] {
返回\"\".join(componentsSeparatedByCharactersInSet(NSCharacterSet.punctuationCharacterSet())).componentsSeparatedByString(\" )
}
}让myWordList =你好,游乐场!单词表// [你好,游乐场]
更新:
扩展字符串{
VAR单词表:[字符串] {
返回componentsSeparatedByCharactersInSet(.punctuationCharacterSet())
.joinWithSeparator()
.componentsSeparatedByString()
}
}
Is it possible to put each word of a string into an array in Swift?
for instance:
var str = "Hello, Playground!"
to:
var arr = ["Hello","Playground"]
Thank you :)
解决方案
extension String {
var wordList: [String] {
return "".join(componentsSeparatedByCharactersInSet(NSCharacterSet.punctuationCharacterSet())).componentsSeparatedByString(" ")
}
}
let myWordList = "Hello, Playground!".wordList // ["Hello", "Playground"]
update: Xcode 7.1.1 • Swift 2.1
extension String {
var wordList: [String] {
return componentsSeparatedByCharactersInSet(.punctuationCharacterSet())
.joinWithSeparator("")
.componentsSeparatedByString(" ")
}
}
这篇关于我怎样才能把一个字符串的每一个单词到斯威夫特的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文