LuaNET, the lua 5.1 api documentation!

Lua-Api

String

[LUA 5.1] string.byte

function string.byte ( s, i, j )
Returns the internal numerical codes of the characters s[i], s[i+1], ···, s[j]. (Defaults: i=1, j=i)
Code
print(string.byte("ABCD",2,2)) -- 66
print(string.byte("ABCD",2,3)) -- 66 67
print(string.byte("ABCD",1,3)) -- 65 66 67


If i or j is negative, it counts the characters from last character backwards.
Code
print(string.byte("ABCD",-2,-2)) -- 67
print(string.byte("ABCD",-2,-1)) -- 67 68
print(string.byte("ABCD",-3,-1)) -- 66 67 68


Note: Numerical codes are not necessarily portable across platforms.

i and j are optional parameters. If except the s no parameter is given, i is set to 1 and j, too.
Code
print(string.byte("ABCD")) -- 65
print(string.byte("")) -- nil, because of empty string


If except the s and a second parameter is given, i and j are set to the value of the second parameter.
Code
print(string.byte("ABCD",3)) -- 67
print(string.byte("ABCD",4)) -- 68
print(string.byte("ABCD",10)) -- nil, because the string is to short


See the function string.char for converting the internal numerical code of characters back to strings.

Comments

© 2007 DracoBlue :: Valid XHTML, CSS, RSS Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC | SourceNet © 2007, DracoBlue :: Page created in 0.257 seconds with 52 queries.