string/find
From Garry's Mod
(Difference between revisions)
m (Added an example) |
m (Updated the example) |
||
Line 41: | Line 41: | ||
|Description=Prevent the player from sending messages with the F word inside | |Description=Prevent the player from sending messages with the F word inside | ||
|Code=hook.Add( "PlayerSay", "PreventTheFWord", function( ply, text ) | |Code=hook.Add( "PlayerSay", "PreventTheFWord", function( ply, text ) | ||
− | local | + | local fStart, fEnd = string.find( text:lower(), "fuck" ) |
− | + | if fStart then | |
− | + | local civilText = string.sub( text, 1, fStart - 1 ) .. "****" .. string.sub( text, fEnd + 1 ) | |
− | return | + | return civilText |
end | end | ||
end ) | end ) | ||
}} | }} |
Revision as of 16:55, 22 May 2017
Contents |
Description
Attempts to find the specified substring in a string, uses Patterns by default.
Arguments
string haystack
The string to search in.
Arguments
string needle
The string to find, can contain patterns if enabled.
Arguments
number startPos=1
The position to start the search from, can be negative start position will be relative to the end position.
Arguments
boolean noPatterns=false
Disable patterns.
Returns
Starting position of the found text
Returns
Ending position of found text
Returns
Matched text for each group if patterns are enabled and used
Examples
Example
Prevent the player from sending messages with the F word inside
hook.Add( "PlayerSay", "PreventTheFWord", function( ply, text ) local fStart, fEnd = string.find( text:lower(), "fuck" ) if fStart then local civilText = string.sub( text, 1, fStart - 1 ) .. "****" .. string.sub( text, fEnd + 1 ) return civilText end end )