This site is supported by donations to The OEIS Foundation.

Template:Count/doc

From OeisWiki
Jump to: navigation, search

This documentation subpage contains instructions, categories, or other information for Template:Count. [<Edit> Template:Count]

[⧼Purge⧽ Template:Count/doc]

The {{count}} string function template returns how many times string {{{2|}}} (default is a single space, i.e. “ ” without the quotes) appears in string {{{1|}}}.

Notes:

  • If the match string is used as a separator, you must add 1 to get the number of separated items/terms/parameters;
  • If the match string is used as a terminator, you get the number of terminated items/terms/parameters.

Usage

{{count|string|match}}

or

{{count|string}} (match defaults to a single space, i.e. “ ” without the quotes)

Tests

The code {{count||,,}} yields 0.   (0 instance of ,,)

The code {{count|b,,,, d,,|,,}} yields 3.   (3 instances of ,,)

The code {{count|b,,, d,,|,,}} yields 2.   (2 instances of ,,) (,, is counted only once in ,,,, not twice, since once a match is found and counted it is considered consumed)

The code {{count|b,, ,, d,,|,,}} yields 3.   (3 instances of ,,)

The code {{count|b,, c,, d,,|,,}} yields 3.   (3 instances of ,,)

The code {{count|,,b,, c,, d,,|,,}} yields 4.   (4 instances of ,,)

The code {{count|a;; b,, c,, d,,|;;}} yields 1.   (1 instance of ;;)

The code {{count|a;; b,, c,, d,,|,,}} yields 3.   (3 instances of ,,)

The code {{count|a;; b,, c,, d,, e,, f,, g|;;}} yields 1.   (1 instance of ;;)

The code {{count|a;; b,, c,, d,, e,, f,, g|,,}} yields 5.   (5 instances of ,,)

Tests: counting spaces

The code
{{count}}
yields 0.   (0 instance of default single space )
The code
{{count|ab}}
yields 0.   (0 instance of default single space )
The code
{{count|a b}}
yields 1.   (1 instance of default single space )
The code
{{count| a b }}
yields 1.   (1 instance of default single space ) (initial and final spaces of a template argument are ignored and are not part of the argument)
The code
{{count|a  b}}
yields 2.   (2 instances of default single space )
The code
{{count|a b c}}
yields 2.   (2 instances of default single space )

Examples

The code {{count|''r''{{sub|0}};; ''m''{{sub|1}}//''r''{{sub|1}},, ''m''{{sub|2}}//''r''{{sub|2}},, ...,, ''m''{{sub|''k''}}//''r''{{sub|''k''}}|,,}} yields:

3

The code {{count|0;; a,, b,, c,, d,, e,, f,, g,, h,, i,, j,, k,, l,, m,, n,, o,, p|,,}} yields:

15

The code {{count|0;; a,, b,, c,, d,, e,, f,, g,, h,, i,, j,, k,, l,, m,, n,, o,, p,, q|,,}} yields:

16

The code {{count|0;; a,, b,, c,, d,, e,, f,, g,, h,, i,, j,, k,, l,, m,, n,, o,, p,, q,, r,, s,, t,, u,, v,, w,, x,, y,, z,,|,,}} yields:

26

The code {{#expr: 1 + {{count|2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}} }} yields:

25

The code {{count|There are sixty-six elephants living in those six hectares since the last twenty-six years.|six}} yields:

4

The code {{count|There are sixty-six elephants living in those six hectares since the last twenty-six years.|e}} yields:

12

Examples: counting spaces

The code
{{count|There are sixty-six elephants living in those six hectares since the last twenty-six years.}}
yields number of single spaces:
13
The code
{{count| There are sixty-six elephants living in those six hectares since the last twenty-six years. }}
yields number of single spaces (initial and final spaces of a template argument are ignored and are not part of the argument):
13
The code
{{count|There  are sixty-six elephants living in those six hectares since the last twenty-six   years.}}
yields number of single spaces (there is a double space and a triple space):
16

Examples: counting words

See {{count words}}.

Code

{{#expr: ( {{#len: {{{1|}}} }} - {{#len: {{#replace: {{{1|}}} |{{{2|}}}|}} }} ) / {{#len: {{#if: {{{2|}}} |{{{2}}}|1}} }} }}

See also