Garry's Mod Wiki

string.gsub

  string, number string.gsub( string string, string pattern, string replacement, number maxReplaces = nil )

Description

This functions main purpose is to replace certain character sequences in a string using Patterns.

Arguments

1 string string
String which should be modified.
2 string pattern
The pattern that defines what should be matched and eventually be replaced.
3 string replacement
In case of a string the matched sequence will be replaced with it.

In case of a table, the matched sequence will be used as key and the table will tested for the key, if a value exists it will be used as replacement.

In case of a function all matches will be passed as parameters to the function, the return value(s) of the function will then be used as replacement.

4 number maxReplaces = nil
Maximum number of replacements to be made.

Returns

1 string
replaceResult
2 number
replaceCount

Example

Replaces "hello" with "hi" in the string "hello there!"

string.gsub("hello there!", "hello", "hi")
Output: hi there!