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.

text.satlSATELLITE 004 Download
Joining, taking away, finding and turning round
satellite.include(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)
}
OUTPUT
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_string and .str)
  • text becomes a whole number with .number (also .as_number and .to_number)
  • .binary (also .bin) and .hex read 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.

convert.satlSATELLITE 004 Download
Converting between text and numbers
satellite.include(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)
}
OUTPUT
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:

joined.satlSATELLITE 004 Download
A string and a number, either way round
satellite.include(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)
}
OUTPUT
n = 4
4 apples
6

Comparing 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.

order.satlSATELLITE 004 Download
Equal, not equal, and in order
satellite.include(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)
}
OUTPUT
true
true
true
true
false

Good 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, \n a new line, \t a tab, \r a 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, .trim and .substring, as well as reading one character with s[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.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *