pxt-calliope/olddocs/js/string.md

2.0 KiB

String

a piece of text.

@parent js/language

A String is a sequence of characters. For the BBC micro:bit, ASCII character codes 32 to 126 are supported; letters, digits, punctuation marks, and a few symbols. All other character codes appear as a ? on the LED screen.

Declare a string

Use the var statement and the assignment operator := to declare a new local string variable. Like this:

let str = "this is a string"

To declare a string using the Touch Develop editor:

  1. Click var (on the Code Keyboard).

  2. Type a name for your new string variable.

  3. Click on the right side of the := operator.

  4. Type " (or click "abc") and then type a string like hello.

  5. Click the check mark.

Your code should look something like this:

let salutation = "Hello"

The function show string

Use show string to display a string on the LED screen. If the string is multiple characters, the string scrolls right to left. The following example displays Hello world! on the micro:bit screen:

basic.showString("Hello world!", 100)

The first parameter of show string specifies the string, and the second parameter specifies the number of milliseconds between scrolling by one LED column - the larger the value, the slower the scroll will be.

More string functions

Want to compare or concatenate strings? Check out the string functions.

Global string variables

Unlike local variables, global variables are accessible across functions and in nested code blocks. To find out how to declare a global string variable, see global variables.

Lessons

letter up

See also

local variables, global variables, string functions, Number, show string