charat

function charat( str, i ) --> charstr

Description

Returns a substring from str that contains one character at the index i.

Parameters

str

The full string.

i

The index

Return Values

charstr

A substring with the length 1 or an empty string if the index is out of range.

Code

--ZFUNC-charat-v1
local function charat( str, i ) --> charstr
   return string.sub( str, i, i )
end

return charat

Examples

local t = require( "taptest" )
local charat = require( "charat" )

t( charat( "abcdef", 1 ), "a" )
t( charat( "abcdef", 3 ), "c" )
t( charat( "abcdef", -2 ), "e" )

t( charat( "abc", 99 ), "" )
t( charat( "abc", -99 ), "" )
t( charat( "", 99 ), "" )

-- ⌘ - u2318 - e2 8c 98 - 226 140 152
t( string.byte( charat( "⌘", 1 ) ), 0xe2 )
t( string.byte( charat( "⌘", 2 ) ), 0x8c )
t( string.byte( charat( "⌘", 3 ) ), 0x98 )

t()

See also