pxt-calliope/olddocs/js/string.md

61 lines
2.0 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# String
2016-04-02 01:22:47 +02:00
a piece of text.
2016-03-26 00:47:20 +01:00
### @parent js/language
2016-04-13 17:27:45 +02:00
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](/device/screen).
2016-03-26 00:47:20 +01:00
### Declare a string
2016-04-13 17:27:45 +02:00
Use the [var statement](/reference/variables/var) and the [assignment operator](/reference/variables/assign) `:=` to declare a new *local* string variable. Like this:
2016-03-26 00:47:20 +01:00
```
let str = "this is a string"
```
2016-04-13 17:27:45 +02:00
To declare a string using the [Touch Develop editor](/js/editor):
2016-03-26 00:47:20 +01:00
1. Click `var` (on the Code Keyboard).
2. Type a name for your new string variable.
2. Click on the right side of the `:=` operator.
3. Type `"` (or click `"abc"`) and then type a string like `hello`.
4. Click the check mark.
Your code should look something like this:
```
let salutation = "Hello"
```
### The function `show string`
2016-04-13 17:27:45 +02:00
Use [show string](/reference/basic/show-string) to display a string on the [LED screen](/device/screen). If the string is multiple characters, the string scrolls right to left. The following example displays `Hello world!` on the micro:bit screen:
2016-03-26 00:47:20 +01:00
```
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
2016-04-13 17:27:45 +02:00
Want to compare or concatenate strings? Check out the [string functions](/reference/types/string-functions).
2016-03-26 00:47:20 +01:00
### Global string variables
2016-04-13 17:27:45 +02:00
Unlike [local variables](/reference/variables/var), global variables are accessible across functions and in nested code blocks. To find out how to declare a global string variable, see [global variables](/js/data).
2016-03-26 00:47:20 +01:00
### Lessons
2016-04-13 17:27:45 +02:00
[letter up](/lessons/letter-up)
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[local variables](/reference/variables/var), [global variables](/js/data), [string functions](/reference/types/string-functions), [Number](/reference/types/number), [show string](/reference/basic/show-string)
2016-03-26 00:47:20 +01:00