String
A String is a sequence of characters.
A string type is more complex than a number or a boolean. Along with its characters a string type contains information about its length an it lets you change any of its characters using an index. Strings can be broken into smaller strings or added on to make larger strings.
Strings can contain letters, numbers, punctuation marks, and other special characters. Strings can have characters from different languages too.
"abcdefg1234*?!"
A string variable is declared by assigning a variable to a string value:
let myString = "My nice new string is here!"
Strings have operations associated with them so that you can change them or work with parts of them.
let myString = "My nice new string is here!"
// make a smaller string
let mySmallString = myString.substr(0, myString.indexOf("g") + 1)
Create a string variable
In the ||variables:Variables||
category of the Toolbox you can create new variable:
Here’s how to create a string variable using the Toolbox:
- Click
||variables:Variables||
in the Toolbox.
- Click on Make a Variable….
- Choose a name for your variable, type it in, and click Ok.
- Drag the new
||variables:set||
block into your code.
- Click on the
||text:Text||
drawer in the Toolbox and find the ||text:" "||
block.
- Drag the
||text:" "||
block into the value slot in of your variable ||variables:set||
block.
Characters you use in strings
Character sets
The available characters to use for a language is called the character set. Each character in the set has a number code to match it with.
To display characters on the LED screen, the micro:bit, uses the “ASCII” character codes of 32
to 126
; letters, digits, punctuation marks, and a few symbols. All other character codes appear as a ?
on the LED screen.
See also
Number