The number of words in a Swift line to calculate the number of words

I want to do a procedure to find out how many words there are in a line, separated by a space, comma or other character. Then add the total later.

I am doing an average calculator, so I want the total amount of data, and then add up all the words.

+4
source share
5 answers

update: Xcode 10.2.x • Swift 5 or later

Using the Foundation method enumerateSubstrings(in: Range) and setting .byWords as parameters:

 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" var words: [Substring] = [] sentence.enumerateSubstrings(in: sentence.startIndex..., options: .byWords) { _, range, _, _ in words.append(sentence[range]) } print(words) // "["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"]\n" print(words.count) // 79 

Or using the native Swift 5 new Character isLetter property and split method:

 let words = sentence.split { !$0.isLetter } print(words) // "["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"]\n" print(words.count) // 80 

StringProtocol for substring support:

 extension StringProtocol { var words: [SubSequence] { return split { !$0.isLetter } } var byWords: [SubSequence] { var byWords: [SubSequence] = [] enumerateSubstrings(in: startIndex..., options: .byWords) { _, range, _, _ in byWords.append(self[range]) } return byWords } } 

 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", "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"] 
+15
source
 let sentences = "Let there be light!" let separated = split(sentences) { contains(",.! ", $0) }.count println(separated) // prints out 4 (if you just want the array, you can omit ".count") 

If you have a specific punctuation condition that you want to use, you can use this code. Also, if you prefer to use only fast codes :).

+3
source

You can try componentsSeparatedByCharactersInset :

 let s = "Let there be light" let c = NSCharacterSet(charactersInString: " ,.") let a = s.componentsSeparatedByCharactersInSet(c).filter({!$0.isEmpty}) // a = ["Let", "there", "be", "light"] 
+2
source

You can try the following options:

 let name = "some name with, space # inbetween" let wordsSeparatedBySpaces = name.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) let wordsSeparatedByPunctuations = name.componentsSeparatedByCharactersInSet(NSCharacterSet.punctuationCharacterSet()) let wordsSeparatedByHashChar = name.componentsSeparatedByString("#") let wordsSeparatedByComma = name.componentsSeparatedByString(",") let total = wordsSeparatedBySpaces.count + wordsSeparatedByPunctuations.count + wordsSeparatedByHashChar.count + wordsSeparatedByComma.count println("Total number of separators = \(total)") 
0
source

You can use regular expression and extension to simplify your code as follows:

 extension String { var wordCount: Int { let regex = try? NSRegularExpression(pattern: "\\w+") return regex?.numberOfMatches(in: self, range: NSRange(location: 0, length: self.utf16.count)) ?? 0 } } let text = "I live in iran and i love Here" print(text.wordCount) // 8 
0
source

Source: https://habr.com/ru/post/1265525/


All Articles