加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用于字数计算的Swift字符串中的字数

发布时间:2020-12-14 04:52:40 所属栏目:百科 来源:网络整理
导读:我想创建一个过程来找出字符串中有多少个单词,用空格,逗号或其他字符分隔.然后将总数加起来. 我正在制作一个平均计算器,所以我想要数据的总数,然后将所有单词加起来. 解决方法 更新: Xcode 8.2.1? Swift 3.0.2 let sentence = "I want to an algorithm that
我想创建一个过程来找出字符串中有多少个单词,用空格,逗号或其他字符分隔.然后将总数加起来.

我正在制作一个平均计算器,所以我想要数据的总数,然后将所有单词加起来.

解决方法

更新: Xcode 8.2.1? Swift 3.0.2

let sentence = "I want to an algorithm that could help find out how many words are there in a string separated by space or comma or some character. And then append each word separated by a character to an array which could be added up later I'm making an average calculator so I want the total count of data and then add up all the words. By words I mean the numbers separated by a character,preferably space Thanks in advance"

let wordList =  sentence.components(separatedBy: .punctuationCharacters).joined().components(separatedBy: " ").filter{!$0.isEmpty}

print(wordList) // [I,want,to,an,algorithm,that,could,help,find,out,how,many,words,are,there,in,a,string,separated,by,space,or,comma,some,character,And,then,append,each,word,array,which,be,added,up,later,Im,making,average,calculator,so,I,the,total,count,of,data,and,add,all,By,mean,numbers,preferably,Thanks,advance]"
print(wordList.count)  // 79
extension String {
    var words: [String] {
        var words: [String] = []
        enumerateSubstrings(in: startIndex..<endIndex,options: .byWords) { word,_,_ in
            guard let word = word else { return }
            words.append(word)
        }
        return words
    }
}

print(sentence.words) // ["I","want","to","an","algorithm","that","could","help","find","out","how","many","words","are","there","in","a","string","separated","by","space","or","comma","some","character","And","then","append","each","word","array","which","be","added","up","later","I'm","making","average","calculator","so","I","the","total","count","of","data","and","add","all","By","mean","numbers","preferably","Thanks","advance"]

Swift 2.x

let sentence = "I want to an algorithm that could help find out how many words are there in a string separated by space or comma or some character. And then append each word separated by a character to an array which could be added up later I'm making an average calculator so I want the total count of data and then add up all the words. By words I mean the numbers separated by a character,preferably space Thanks in advance"

let wordList =  sentence.componentsSeparatedByCharactersInSet(NSCharacterSet.punctuationCharacterSet()).joinWithSeparator("").componentsSeparatedByString(" ").filter{$0 != ""}

print(wordList) // [I,advance]"
print(wordList.count)  // 79

问题是得到单词的计数,但是如果你需要这些单词,你可以使用这个扩展改编自Martin R在这个answer中建议的方法也能够处理撇号:

extension String {
    var words: [String] {
        var result:[String] = []
        enumerateSubstringsInRange(characters.indices,options: .ByWords) { result.append($0.substring!) }
        return result
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读