Skip to content
everloot

Lore archive · Warcraft Wiki, 2023 dump

CountChars

Last wiki revision 2009-01-27

Return how many times needle is contained in haystack. number = <PREFIX>_CountChars(haystack, needle)

  • haystack: String - The search subject.
  • needle: The string to look for. needle can be a regular expression ('[a-z%s%d]' will count all alphanumeric characters and white spaces)
  • Notes
  • Arguments are cast into strings before any processing is done.
  • The search is not case-sensitive.
  • number: Number of times needle is contained in haystack. If needle is not found, returns 0.

number1 = &lt;PREFIX&gt;_CountChars("World of Warcraft", 'o') number2 = &lt;PREFIX&gt;_CountChars("World of Warcraft", 'w[a-z]') number3 = &lt;PREFIX&gt;_CountChars("World of Warcraft", 'r[a-z]') number4 = &lt;PREFIX&gt;_CountChars("World of Warcraft", 'r[a-z][a-z]')

number1 = 2 number2 = 2 number3 = 3 number4 = 3

function <PREFIX>_CountChars (haystack, needle) haystack = strlower(tostring(haystack)); needle = strlower(tostring(needle)); local ret, pos = 0, 0 for i = 1, strlen(haystack) do local s, e = strfind(haystack, needle, pos) if e then pos = e if s == e then pos = pos + 1 end end if s and e then ret = ret + 1; else break -- If it hasn't found any more, there's no reason to continue end end return ret end

Categories

  • User defined functions

Source

https://warcraft.wiki.gg/wiki/CountChars · retrieved 2023-11-13 · CC BY-SA 4.0

Converted from wikitext with templates, tables and images removed, so some sentences may be missing values.

This archive predates WoW: Forever. For collected gameplay facts see the Forever database. Search the archive.