From Test-Scratch-Wiki
(Redirected from Eng:Strings)
Strings are sequences of binary-encoded computer characters, including spaces. More commonly known as letters and numbers, they can be of any length up to 10240 characters (unless special techniques are used[1]), include any typable character, and be put to work in certain blocks, mostly found in the Operators section. String support was added in Scratch 1.4.[2]
Example Strings
Below are some examples that express the theoretically infinite range of strings.
- A string
say [Hello world!]
- Addressing a user by their username
when gf clicked say (join (join [Hi, ] (username)) [!])
- A string with a variable inside
when gf clicked if <(score) < (3)> say (join (join [Your score was ] (score)) [. Better luck next time!]) else say (join (join [Your score was ] (score)) [! Keep it up!])
- The empty string or null string is a string with no characters
set [test v] to [] // Empty set [test v] to (join (test) [a]) // Now test is "a"
Blocks that can Handle Strings
Several blocks use strings; below is a full list:
String Reporters
Certain blocks can make use of strings, forming new ones or reading aspects of them, such as its character length.
- Join ()(): Joins two strings together with no space in between (to connect words, one can add spaces at the end of or the beginning of one of the strings; this can also be used to convert integers to strings)
- Letter () of (): Reports the numbered character of a given string
- Length of (): Reports the length of a given string
- Answer: Reports the string input requested from the Ask () and Wait block
- (variable):reports the string or value stored within a variable
- (list):reports a string consisting of all items of a list combined in numerical order
String Inserts
Other blocks take string input and keep it.
- Say () for () secs: Takes a string insert to determine what to say
- Say (): Takes a string insert to determine what to say
- Think () for () secs: Takes a string insert to determine what to think
- Think (): Takes a string insert to determine what to think
- Ask () and wait: Asks the imputed string, then collects a new one in the answer block.
- Join ()(): Joins two strings together with no space in between (to connect words, one can add spaces at the end of or the beginning of one of the strings)
- Letter () of (): Reports the numbered character of a given string
- Length of (): Reports the length of a given string
- Set () to (): The variable is a string
- Add () to (): Adds a string to a list
- Insert () at () of (): Inserts a string at a given place in a list
- Replace item () of () With (): Replaces an item at a given spot in a list with a string
- () Contains (): Checks if the list has a string
String or Number?
In Scratch, strings are casted into 0 when put into a number slot. This can only be done by using the variable, answer, or item () of () blocks. However, sometimes a string is not wanted in a number insert, such as in an OS clock. This is a simple script that detects whether or not something is a string or a number:
if <<((reporter) + (1)) = [1]> and <not <(reporter) = [0]>>> then//Strings have a reporter value of 0 say [String!] else say [Number!] end
Alternatively, in BYOB, the block <is [] a [number/text]?> can be used.
Technicality
All data a computer reads is encoded into binary code, or a series of "1"s and "0"s. Each individual "1" or "0" is called a bit and 8 of these make up a byte. Different sequences of bits and bytes resemble codes for different standardized letters. This is known as an encoding, or the uniform way of breaking down letters into sequences of numbers. Different encodings use different amounts of bits for each letter, and it all depends on how many characters would be available for a single, uniform encoding.
In Scratch, a string is simply a series of encoded letters that is parsed with a particular decoder. A string of text is read by the computer as plain text as opposed to compiled code for the instructions to a central processing unit.
See Also
- String on Wikipedia