string/find

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Also menu state)
m (Added an example)
Line 3: Line 3:
 
|Realm=Shared and Menu
 
|Realm=Shared and Menu
 
|IsClass=No
 
|IsClass=No
|Name=find
 
|Parent=string
 
 
}}
 
}}
 
{{Arg
 
{{Arg
Line 39: Line 37:
 
|type=string
 
|type=string
 
|desc=Matched text for each group if patterns are enabled and used
 
|desc=Matched text for each group if patterns are enabled and used
 +
}}
 +
{{Example
 +
|Description=Prevent the player from sending messages with the F word inside
 +
|Code=hook.Add( "PlayerSay", "PreventTheFWord", function( ply, text )
 +
local text = text:lower()
 +
if string.find( text, "fuck" ) then
 +
ply:ChatPrint( "Please be civil in the chat" )
 +
return ""
 +
end
 +
end )
 
}}
 
}}

Revision as of 16:52, 22 May 2017

 string.find( )

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

number

Starting position of the found text

Returns

number

Ending position of found text

Returns

string

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 text = text:lower()
	if string.find( text, "fuck" ) then
		ply:ChatPrint( "Please be civil in the chat" )
		return ""
	end
end )


Personal tools
Navigation