String.count: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(Simplify the function) |
||
Line 13: | Line 13: | ||
== Code == | == Code == | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function string.count(text, search) | function string.count (text, search) | ||
if | if ( not text or not search ) then return false end | ||
return select ( 2, text:gsub ( search, "" ) ); | |||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 29: | Line 23: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
outputChatBox(string.count(" | outputChatBox( string.count( "Just a test string", "t" ) ) -- and the result is 4! | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Created by Krischkros, Edited by nextz. |
Revision as of 02:17, 5 March 2016
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 ( not text or not search ) then return false end return select ( 2, text:gsub ( search, "" ) ); end
Usage:
outputChatBox( string.count( "Just a test string", "t" ) ) -- and the result is 4!
Created by Krischkros, Edited by nextz.