我想将文本拆分成一个数组,保持标点符号被其余单词分隔,所以字符串如下:
Hello, I am Albert Einstein.
应该变成这样的数组:
["Hello", ",", "I", "am", "Albert", "Einstein", "."]
我尝试过使用sting.components(separatedBy:CharacterSet.init(charactersIn:“,;;:”))但是这个方法删除所有标点符号,并返回如下数组:
["Hello", "I", "am", "Albert", "Einstein"]
那么,我怎样才能得到像我的第一个例子一样的数组?
最佳答案 它不是很好的解决方案,但你可以尝试:
var str = "Hello, I am Albert Einstein."
var list = [String]()
var currentSubString = "";
//enumerate to get all characters including ".", ",", ";", " "
str.enumerateSubstrings(in: str.startIndex..<str.endIndex, options: String.EnumerationOptions.byComposedCharacterSequences) { (substring, substringRange, enclosingRange, value) in
if let _subString = substring {
if (!currentSubString.isEmpty &&
(_subString.compare(" ") == .orderedSame
|| _subString.compare(",") == .orderedSame
|| _subString.compare(".") == .orderedSame
|| _subString.compare(";") == .orderedSame
)
) {
//create word if see any of those character and currentSubString is not empty
list.append(currentSubString)
currentSubString = _subString.trimmingCharacters(in: CharacterSet.whitespaces )
} else {
//add to current sub string if current character is not space.
if (_subString.compare(" ") != .orderedSame) {
currentSubString += _subString
}
}
}
}
//last word
if (!currentSubString.isEmpty) {
list.append(currentSubString)
}
在Swift3中:
var str = "Hello, I am Albert Einstein."
var list = [String]()
var currentSubString = "";
//enumerate to get all characters including ".", ",", ";", " "
str.enumerateSubstrings(in: str.startIndex..<str.endIndex, options: String.EnumerationOptions.byComposedCharacterSequences) { (substring, substringRange, enclosingRange, value) in
if let _subString = substring {
if (!currentSubString.isEmpty &&
(_subString.compare(" ") == .orderedSame
|| _subString.compare(",") == .orderedSame
|| _subString.compare(".") == .orderedSame
|| _subString.compare(";") == .orderedSame
)
) {
//create word if see any of those character and currentSubString is not empty
list.append(currentSubString)
currentSubString = _subString.trimmingCharacters(in: CharacterSet.whitespaces )
} else {
//add to current sub string if current character is not space.
if (_subString.compare(" ") != .orderedSame) {
currentSubString += _subString
}
}
}
}
//last word
if (!currentSubString.isEmpty) {
list.append(currentSubString)
}
我们的想法是循环所有角色并同时创建单词.一个单词是一组连续的字符,不是,.要么 ;.因此,在创建循环中的单词期间,如果我们看到其中一个字符,则我们完成当前单词,并且构造中的当前单词不为空.
要根据您的输入细分步骤:
>得到H(不是空格或其他终端字符)
– > currentSubString =“H”
>获得e(不是空格或其他终端字符)
– > currentSubString =“他”
>得到l(不是空格或其他终端字符)
– > currentSubString =“Hel”
>得到l(不是空格或其他终端字符)
– > currentSubString =“地狱”
>得到o(不是空格或其他终端字符)
– > currentSubString =“你好”
>得到. (是终端角色)
> – >因为currentSubString不为空,添加到列表并重新构造下一个单词,然后list = [“Hello”]
> – > currentSubString =“.” (我使用修剪的原因只是为了删除,如果我得到这个角色.但对于其他终端角色,我们必须保留下一个字.
> get(是空格字符)
> – >由于currentSubString不为空,添加到列表并重新开始构建 – > list = [“你好”,“.”]
> – > currentSubString =“”(修剪).
… 等等.