Fandom

Pico-8 Wiki

Sub

162pages on
this wiki
Add New Page
Comments0 Share
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 Edit

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 Edit

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.