string.find

From Garry's Mod
Jump to: navigation, search
 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, or nil if the text wasn't found

Returns

number

Ending position of found text, or nil if the text wasn't found

Returns

string

Matched text for each group if patterns are enabled and used, or nil if the text wasn't found

Examples

Example

Change the word "heck" to "****" in chat messages

hook.Add( "PlayerSay", "NoHeckHere", function( ply, text )
	local heckStart, heckEnd = string.find( text:lower(), "heck" )
	if heckStart then
		local civilText = string.sub( text, 1, heckStart - 1 ) .. "****" .. string.sub( text, heckEnd + 1 )
		return civilText
	end
end )


Personal tools
Navigation