Skip to content

ProtoString wordCount() Method

Seth Clydesdale edited this page Jun 6, 2015 · 1 revision

<< Back to Miscellaneous Methods

Examples

Leaving the parameter undefined or filling it in as "number" will return the total number of words.

'I love coffee. ♥'.wordCount(); // returns 3
'I love coffee. ♥'.wordCount('number'); // returns 3

Filling in the parameter as "array" will return an array with all words that occur in the string. As an array, the total number of words can be accessed via the length property

'I love coffee. ♥'.wordCount('array'); // returns ['I', 'love', 'coffee']

Filling in the parameter as "object" will return the words as an object. The total number of words can then be accessed via the words property

'I love coffee. ♥'.wordCount('object'); // returns {0:'I', 1:'love', 2:'coffee', words:3}

Description

The wordCount method returns the total number of words in a string. The return value can be the number of words, or an array or object which contains each word.

Syntax

String.wordCount(type);

Parameters

Parameter Description
Type A string representing the data type to be returned. Possible values are number, array, and object. Defaults to number if left undefined.
Clone this wiki locally