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( "tapered" )
local charat = require( "charat" )

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

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

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

t.done()

See also