PICO-8 Wiki
(New reference article: sub)
Tags: Visual edit apiedit
 
No edit summary
Tags: Visual edit apiedit
Line 20: Line 20:
 
function numtochar(v)
 
function numtochar(v)
 
return sub(chars, v, v)
 
return sub(chars, v, v)
  +
end
 
</pre>
 
</pre>
   

Revision as of 08:17, 18 June 2016

sub( str, start, [end] )
Gets the substring of a string.
str
The string.

start
The starting index, counting from 1.

end
The ending index, counting from 1, inclusive. If omitted, the rest of the string is returned.

Examples

print(sub("hello there", 1, 5))  -- hello
print(sub("hello there", 7))     -- there

Pico-8 has no built-in way to associate letters with numbers. You can simulate this using a string as a look-up table:

chars = ' !"#%\'()*+,-./0123456789:;<=>?abcdefghijklmnopqrstuvwxyz[]^_{~}'

function numtochar(v)
  return sub(chars, v, v)
end

Converting a character back a number is a bit more cumbersome but also uses sub() and the lookup table:

function chartonum(c)
  local i
  for i=1,#chars do
    if sub(chars, i, i) == c then
      return i
    end
  end
  return 0
end

See also