string.sub

From GMod Wiki

Jump to: navigation, search
Function
Syntax string.sub( String Str, Integer Start[, Integer End] )
Where is this used?
Description:
Returns a substring of the string passed. The third parameter is NOT the length, this is a common mistake - it is a position in the string like Start.
Returns: String
Part of Library: String
Realm: NewerShared.png
BBCode Link: [b][url=http://wiki.garrysmod.com/?title=String.sub]String.sub [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]



Example

DescriptionExamples of possible ways to use string.sub.
Used onNewerShared.png
Code
teststring = "Hello, world!"
 
// from character 8 to the end
print(string.sub(teststring, 8))
 
// from character 2 to 5 (inclusive)
print(string.sub(teststring, 2, 5))
 
// 6th character from the end, to the end
print(string.sub(teststring, -6))
 
// 6th character from the end to the 5th character from the end
print(string.sub(teststring, -6, -5))
 
// 6th character from the end to the 12th character from the start
print(string.sub(teststring, -6, 12))
Outputworld!

ello
world!
wor

world


Additional Notes


See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox