Difference between revisions of "String.sub"

From GiderosMobile
 
(2 intermediate revisions by one other user not shown)
Line 5: Line 5:
 
=== Description ===
 
=== Description ===
 
Returns the substring of s that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length). In particular, the call string.sub(s,1,j) returns a prefix of s with length j, and string.sub(s, -i) returns a suffix of s with length i.
 
Returns the substring of s that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length). In particular, the call string.sub(s,1,j) returns a prefix of s with length j, and string.sub(s, -i) returns a suffix of s with length i.
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
(string) = string.sub(s,i,j)
 
(string) = string.sub(s,i,j)
</source>
+
</syntaxhighlight>
  
 
=== Parameters ===
 
=== Parameters ===
Line 17: Line 17:
 
'''Returns''' (string) substring<br/>
 
'''Returns''' (string) substring<br/>
  
=== Example ===
+
=== Examples ===
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
local mynme = "nme01"
 
local mynme = "nme01"
 
print(string.sub(mynme, 1, 3)) -- nme
 
print(string.sub(mynme, 1, 3)) -- nme
 
-- or this
 
-- or this
 
print(mynme:sub(1, 3)) -- nme
 
print(mynme:sub(1, 3)) -- nme
</source>
+
</syntaxhighlight>
 +
 
 +
'''Omitting the optional parameter'''
 +
<syntaxhighlight lang="lua">
 +
local mynme = "nmeWolf"
 +
print(mynme:sub(4)) -- Wolf
 +
local mynme2 = "nmeElephant"
 +
print(mynme2:sub(4)) -- Elephant
 +
</syntaxhighlight>
  
 
{{String}}
 
{{String}}

Latest revision as of 07:17, 21 July 2025

Available since: Gideros 2011.6
Class: string

Description

Returns the substring of s that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length). In particular, the call string.sub(s,1,j) returns a prefix of s with length j, and string.sub(s, -i) returns a suffix of s with length i.

(string) = string.sub(s,i,j)

Parameters

s: (string) string to get substring from
i: (number) start index of substring
j: (number) index of the last character of substring optional

Return values

Returns (string) substring

Examples

local mynme = "nme01"
print(string.sub(mynme, 1, 3)) -- nme
-- or this
print(mynme:sub(1, 3)) -- nme

Omitting the optional parameter

local mynme = "nmeWolf"
print(mynme:sub(4)) -- Wolf
local mynme2 = "nmeElephant"
print(mynme2:sub(4)) -- Elephant