Garry's Mod Wiki

string.find

  number, number, string string.find( string haystack, string needle, number startPos = 1, boolean noPatterns = false )

Description

Attempts to find the specified substring in a string.

This function uses Lua Patterns by default.

Arguments

1 string haystack
The string to search in.
2 string needle
The string to find, can contain patterns if enabled.
3 number startPos = 1
The position to start the search from, can be negative start position will be relative to the end position.
4 boolean noPatterns = false
Disable patterns.

Returns

1 number
Starting position of the found text, or nil if the text wasn't found
2 number
Ending position of found text, or nil if the text wasn't found
3 string
Matched text for each group if patterns are enabled and used, or nil if the text wasn't found

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 )