Discord
Login
Community
DARK THEME

string.charAt(num)

Returns the character at the number place.Like this.

init = function()
  myString = "Hello!"
end

draw = function()
  screen.clear()
  screen.drawText(myString.charAt(4),0,0,20,"#FFF")
end

This will draw the second "l" from "Hello!".

That is (kind off) already possible in microStudio. Strings are treated as lists and single characters can be accessed by their index.

Like so:

init = function()
  text = "Hello World"
end

draw = function()
  screen.clear()
  screen.drawText(text[6],0,0,8,"#FFF")
end

Note: The index for the first character is 0. So above example would give you the "W".


Live Long And Tinker

That triggered my tinker instincts, couldn't resist, ... allows you to do fun stuff like this:

:embed https://microstudio.io/TinkerSmith/wave/

Link: https://microstudio.dev/i/TinkerSmith/wave/

Oh,I didn't know about that.

Also strings in microScript currently mostly rely on the JavaScript string type ; thus you can use any string function that exists in JavaScript and it should work, like charAt or charCodeAt. Only the functions that are officially documented are guaranteed to keep being supported though (I mean when microScript will be compiled / translated to other languages one day, those undocumented functions should not be ported if they aren't official ; that's not really a concern for now).

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community