string/gsub

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Also menu state)
m (maxReplaces is optional, gave it a default nil value)
 
Line 1: Line 1:
 
{{Func
 
{{Func
|Name=gsub
 
|Parent=string
 
 
|Description=This functions main purpose is to replace certain character sequences in a string using [[Patterns]].
 
|Description=This functions main purpose is to replace certain character sequences in a string using [[Patterns]].
 
|Realm=Shared and Menu
 
|Realm=Shared and Menu
Line 29: Line 27:
 
|name=maxReplaces
 
|name=maxReplaces
 
|desc=Maximum number of replacements to be made.
 
|desc=Maximum number of replacements to be made.
 +
|default=nil
 
}}
 
}}
 
{{Ret
 
{{Ret
Line 36: Line 35:
 
{{Ret
 
{{Ret
 
|type=number
 
|type=number
|desc=replaceCount  
+
|desc=replaceCount
 
}}
 
}}
 
{{Example
 
{{Example
 
|Description=Replaces "hello" with "hi" in the string "hello there!"
 
|Description=Replaces "hello" with "hi" in the string "hello there!"
 
|Code=string.gsub("hello there!", "hello", "hi")
 
|Code=string.gsub("hello there!", "hello", "hi")
 
 
|Output=hi there!
 
|Output=hi there!
 
}}
 
}}

Latest revision as of 04:40, 6 July 2017

 string.gsub( )

Contents

Description

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

Arguments

string string

String which should be modified.

Arguments

string pattern

The pattern that defines what should be matched and eventually be replaced.

Arguments

string replacement

In case of a string the matches 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.

Arguments

number maxReplaces=nil

Maximum number of replacements to be made.

Returns

string

replaceResult

Returns

number

replaceCount

Examples

Example

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

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

Output:

hi there!
Personal tools
Navigation