没有得到。在斯威夫特平均计算的话 [英] Get no. Of words in swift for average calculator

查看:118
本文介绍了没有得到。在斯威夫特平均计算的话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个算法可以帮助找出多少字由空格或逗号或某些字符分隔字符串在那里。然后用字符分隔每个字附加到这可能是后相加的阵列
我在做一个平均计算,所以我想数据的总数,然后加起来所有的话。
通过话,我的意思是一个字符,preferably空格分隔的数字
在此先感谢


解决方案

 让句子=我想一个算法可以帮助找出多少单词通过分隔字符串在那里空格或逗号或某些字符。然后追加一个字符来,所以我想数据的总数,然后加起来所有的话可能被加了后,我正在做一个平均计算阵列分隔每个单词。通过话我的意思是一个字符,preferably空间提前感谢分离数字让单词表= sentence.componentsSeparatedByCharactersInSet(NSCharacterSet.punctuationCharacterSet()).joinWithSeparator(\"\").componentsSeparatedByString(\" ).filter {$ 0!=}打印(单词表)// [我,要,要,一,算法,即,能,帮助寻找,出来了,怎么很多,也就是说,是有,中,A,串,分离,由,空间,或,逗号或一些,性格,而且,当时,追加,每,字分开,由一个,性格,于一,数组,其中,可以,可以,补充说,起来,后来,IM,制作,一个,平均值,计算器,所以,我,想的,总,计数,数据,和,然后,添加时,所有的,即,通过,即,I,意思是,,数字,分离,由,一,性格,preferably,空间,谢谢你,在,提前]
打印(wordList.count)// 79

问题是将文字的计数,但如果你需要的话,因为他们,你可以使用这个扩展改编自本的回答做能够处理单引号也:

 扩展字符串{
    VAR的话:[字符串] {
        VAR结果:[字符串] = []
        enumerateSubstringsInRange(characters.indices,选择:.ByWords){result.append($ 0.substring!)}
        返回结果
    }
}打印(sentence.words.description)// [I,希望,来,一个,算法,认为,可能,帮助,查找,走出去 怎么样,多,字,有,有,中,一,串,分离,靠,空间,或,逗号,或,一些,性格,和,然后,追加,每一个,字,分离,靠,A,八字 为,一个,阵,这,可能,将,补充,上,后来,我,制造,一个 ,平均,计算器,所以,I,希望,中,数据,和,那么,中,总,伯爵,添加,上,全,中,字,通过,字,I,中庸,中,数字,分离,靠 一,人物,preferably,中,空间,谢谢,前进]

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 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, 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, Im, 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]"
print(wordList.count)  // 79

The question is to get the count of words but if you need the words as they are, you can use this extension adapted from the method suggested by Martin R in this answer do be able to deal with apostrophes also:

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

print(sentence.words.description) // ["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"]

这篇关于没有得到。在斯威夫特平均计算的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆