Garry's Mod Wiki

Revision Difference

string.sub#568549

<function name="sub" parent="string" type="libraryfunc"> <description>Returns a substring, 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> <realm>Shared and Menu</realm> <args> <arg name="string" type="string">The string you'll take a sub-string out of.</arg> <arg name="StartPos" type="number">The position of the first character that will be included in the substring. It can be negative to count from the end.</arg> <arg name="EndPos" type="number" default="nil">The position of the last character to be included in the substring. It can be negative to count from the end. In case this argument is omitted, all the characters to the end are returned.</arg> </args> <rets> <ret name="" type="string">The substring.</ret> </rets> </function> <example> <description>Demonstrates the use of this function.</description> <code> local mystring = "Some random text" print( string.sub( mystring, 6 ) ) -- `random text`⤶ print( string.sub( mystring, 1, 4 ) ) -- `Some`⤶ print( string.sub( mystring, 1, -6 ) ) -- `Some random`⤶ print( string.sub( mystring, -4, -1 ) ) -- `text`⤶ print( string.sub( mystring, 6 ) )⤶ print( string.sub( mystring, 1, 4 ) )⤶ print( string.sub( mystring, 1, -6 ) )⤶ print( string.sub( mystring, -4, -1 ) )⤶ </code> <output>⤶ ```⤶ random text⤶ Some⤶ Some random⤶ text⤶ ```⤶ </output>⤶ </example>