Try This: Check String Length
If your project uses an index that’s larger than the string, it’ll cause an exception. To find out how many characters are in a string, Makecode has that built-in function called length of () that returns the number of characters in a string. Your project can check a string’s length, then can use that result in indexing when accessing characters in that string, so that it never tries to access an out of bounds character. This is especially important when each character is indexed in a loop.
Before indexing all the characters in a string, let’s use length of () to verify that there are 12 characters in the string.
Example project: char_access_try_this
- Enter and name char_access_try_this.
- Click the Download button.
- Check the results in the serial terminal.
- Verify that it displays length = 12.