String.count

From Multi Theft Auto: Wiki
Revision as of 23:28, 2 March 2016 by DABL (talk | contribs)
Jump to navigation Jump to search

This function counts how many times a string is found from within another string.

Syntax

int string.count(string text, string search)

Required Arguments

  • text: A string to find the text from
  • search: A string defining what to find

Code

function string.count(text, search)
	if search and text then
		local string_count = 0
		for i in string.gfind(text, search) do
			string_count = string_count + 1
		end
		return string_count
	else
		return 0
	end
end

Usage:

outputChatBox(string.count("A Text", "T")) -- and the result is 2!

Code and Edit by Krischkros.