string/sub

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Also menu state)
m (Added note for possibility of negative EndPos)
 
Line 1: Line 1:
 
{{Func
 
{{Func
|Name=sub
 
|Parent=string
 
 
|Description=Returns a sub-string, starting from the character at position ''StartPos'' of the string (inclusive), and optionally ending at the character at position ''EndPos'' of the string (also inclusive). If EndPos is not given, the rest of the string is returned.
 
|Description=Returns a sub-string, starting from the character at position ''StartPos'' of the string (inclusive), and optionally ending at the character at position ''EndPos'' of the string (also inclusive). If EndPos is not given, the rest of the string is returned.
 
|Realm=Shared and Menu
 
|Realm=Shared and Menu
Line 19: Line 17:
 
|type=number
 
|type=number
 
|name=EndPos
 
|name=EndPos
|desc=The position of the last character to be included in the sub-string.
+
|desc=The position of the last character to be included in the sub-string. It can be negative to count from the end.
 
|default=nil
 
|default=nil
 
}}
 
}}

Latest revision as of 02:14, 6 May 2017

 string.sub( )

Contents

Description

Returns a sub-string, starting from the character at position StartPos of the string (inclusive), and optionally ending at the character at position EndPos of the string (also inclusive). If EndPos is not given, the rest of the string is returned.

Arguments

string string

The string you'll take a sub-string out of.

Arguments

number StartPos

The position of the first character that will be included in the sub-string.

Arguments

number EndPos=nil

The position of the last character to be included in the sub-string. It can be negative to count from the end.

Returns

string

The substring.

Examples

Example

Demonstrates the use of this function.

local mystring = "Some random text"
print(string.sub(mystring, 1, 4))

Output:

Some
Personal tools
Navigation