String.count: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (dont mix languages)
Line 1: Line 1:
English:
This Function counts a text from a text.
This Function counts a text from a text.
It is server side and client side!
It is server side and client side!
Line 22: Line 20:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
outputChatbox(string.count("A Text", "T")) -- and the result is 2!
outputChatbox(string.count("A Text", "T")) -- and the result is 2!
</syntaxhighlight>
German:
Diese Funktion zählt euch auf, wie oft ein String in einem anderen gegeben String vorhanden ist.
Diese Funktion geht serverseitig, wie auch clientseitig.
Die Verwendung ist eigentlich ganz simpel, allerdings sehr nützlich!
<syntaxhighlight lang="lua">
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
</syntaxhighlight>
Verwendung:
<syntaxhighlight lang="lua">
outputChatbox(string.count("Yeah, Senfsalat", "a")) -- Das Ergebnis btw. der Rückgabewert währe in diesem Fall 3. Da 3x "a" vorhanden ist.
</syntaxhighlight>
</syntaxhighlight>


Code and Edit by Krischkros.
Code and Edit by Krischkros.

Revision as of 23:56, 24 July 2012

This Function counts a text from a text. It is server side and client side!

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.