TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to RUBY

Ruby Word Count: Split Method

Use the split method to count words in strings. Split on non-word characters.
Word count. A string contains multiple words. Each word is separated by non-word characters. So each word can be matched with the pattern "\s+." We split words apart and then return the resulting array's length.
Example. We introduce the wordcount method. It receives one value, a string, and returns the length of an array. The split method splits words apart, treating each sequence of whitespace characters as a delimiter.

Then: I test wordcount with four example strings. I verified that the strings contain the same number of words the method indicates.

Info: The whitespace-only string and the empty string should both contain zero words. The result is as expected.

Ruby program that counts words def wordcount(value) # Split string based on one or more whitespace characters. # ... Then return the length of the array. value.split(/\s+/).length end value = "To be or not to be, that is the question." puts wordcount(value) value = "Stately, plump Buck Mulligan came from the stairhead" puts wordcount(value) puts wordcount " " puts wordcount "" Output 10 8 0 0
Split is one of the more powerful regexp methods. It does not match just one location, like match does. It matches all possible parts of the string and returns an array of the results.

So: It may not be intuitive to use split to count words, but this approach is effective and requires little extra code.

Split
A loop-based method could be faster. It would result in fewer allocations. But this would also introduce further complexity into a program. Often the extra complexity causes more problems.
Summary. When counting words, whitespace chars, along with punctuation, must be treated as non-word characters. We must consider them together, not alone. And with the "\s+" pattern, we can effectively do this.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf