Difference between revisions of "Charcount"

From Sega Retro

(Created page with "<includeonly>{{#vardefine:charcountresult|0}}{{#loop: subchar |0 |{{#len:{{{1}}}}} |{{#ifeq: {{#sub:{{{1}}}|{{#var:subchar}}|{{#len:{{{2}}}}}}} | {{{2}}} | {{incvar|charcountr...")
 
(faster)
Line 1: Line 1:
<includeonly>{{#vardefine:charcountresult|0}}{{#loop: subchar
+
<includeonly>{{#vardefine:charcountresult|-1}}{{#vardefine:charpos|0}}{{#while:
|0
+
|{{#if:{{#var:charpos}}|true}}
|{{#len:{{{1}}}}}
+
|{{#vardefine:charpos|{{#pos:{{{1}}}|{{{2}}}|{{#expr:{{#var:charpos}} + 1}}}}}}{{incvar|charcountresult}}
|{{#ifeq: {{#sub:{{{1}}}|{{#var:subchar}}|{{#len:{{{2}}}}}}} | {{{2}}} | {{incvar|charcountresult}} }}
 
 
}}{{#var:charcountresult}}
 
}}{{#var:charcountresult}}
 
</includeonly><noinclude>
 
</includeonly><noinclude>

Revision as of 09:17, 5 June 2018

This template counts the instances of a specified character (or another string) in a string. Note that it is case-sensitive.

Usage

{{charcount|She sells sea shells.|e}}


returns:

4