STRINGS
A string is a piece of text: a name, a sentence, a line from a file. In satellite it is satellite.variable.string, and you write its value between double quotes.
satellite keeps a string as characters, not as bytes. An accented letter, an arrow or a Japanese character is one character, so turning a string round works the way you would expect, in any language.
Joining, taking away and finding
+ joins two strings into one, and .add(x) does the same. - takes away the first place the right-hand string appears, and leaves the string unchanged when it is not there. .find(x) tells you where x first begins, counting from 0. .reverse() turns the string round.
(satellite)
satellite.capsule satellite.main()
{
satellite.variable.string s = "hello world"
satellite.console.display(s + "!")
satellite.console.display(s.add("!"))
satellite.console.display(s - "o")
satellite.console.display(s - "z")
satellite.console.display(s.find("world"))
satellite.console.display(s.reverse())
satellite.console.display("héllo→".reverse())
satellite.console.display("日本語".reverse())
satellite.return(satellite)
}hello world!
hello world!
hell world
hello world
6
dlrow olleh
→olléh
語本日s - "o" took away only the first “o”, from “hello”, and left the one in “world”. If .find cannot find what you asked for, it does not answer -1 the way some languages do: it stops with S420, “did not find it”, so a missing piece of text never passes silently as a number.
Strings and numbers
A number added to a string joins it as its digits: "n = " + 4 is n = 4, and so is "n = " + 4.string. With the number first, text that spells a number is added — 4 + "2" is the number 6 — and any other text is joined: 4 + " apples" is 4 apples. Every other conversion you say out loud:
- a number becomes text with
.string(also.to_stringand.str) - text becomes a whole number with
.number(also.as_numberand.to_number) .binary(also.bin) and.hexread the number in the text and answer its digits in base 2 or base 16, as a string
Methods chain from left to right, so "87".bin.find("111") reads 87, writes it in binary, and looks for “111” in that.
(satellite)
satellite.capsule satellite.main()
{
satellite.variable.number n = 4
satellite.console.display("n = " + n.to_string())
satellite.console.display("42".number + 1)
satellite.console.display("42".bin)
satellite.console.display("255".hex)
satellite.console.display("87".bin)
satellite.console.display("87".bin.find("111"))
satellite.return(satellite)
}n = 4
43
101010
FF
1010111
4.number reads only a whole number. "3.5".number is refused (S120), because satellite does not round anything unless you ask it to.
Here is the join with nothing written out, both ways round:
(satellite)
satellite.capsule satellite.main()
{
satellite.variable.number n = 4
satellite.console.display("n = " + n)
satellite.console.display(n + " apples")
satellite.console.display(n + "2")
satellite.return(satellite)
}n = 4
4 apples
6Comparing strings
== and != ask whether two strings are the same. < and > put them in order using satellite’s own alphabet: the small letters a to z first, then the capitals A to Z, then the digits, then other signs, then the space. So "a" < "B" is true, and "Z" < "a" is false.
(satellite)
satellite.capsule satellite.main()
{
satellite.variable.string fruit = "apple"
satellite.console.display(fruit == "apple")
satellite.console.display(fruit != "pear")
satellite.console.display("apple" < "banana")
satellite.console.display("a" < "B")
satellite.console.display("Z" < "a")
satellite.return(satellite)
}true
true
true
true
falseGood to know
- A string can carry colour:
"OK".foreground(x00FF00)is the word OK in green when it is shown in a terminal. See COLOR ON THE CONSOLE. - A backslash starts an escape:
\"is a double quote,\\a backslash,\na new line,\ta tab,\ra carriage return and\'an apostrophe, so"tab\there"shows tab, a tab, and here. Any other backslash stays in the text as you wrote it. - Several string methods are named in the language and not built yet, and satl refuses each one today: among them
.size,.upper,.lower,.split,.replace,.trimand.substring, as well as reading one character withs[n].
Where next
The FULL REFERENCE lists every string word. For putting text on the screen, read DISPLAYING TEXT, and for turning numbers into text and back, read NUMBERS.
