Strings
In the previous section (Numbers), we learned that the simplest Sonar program is really just a number.
>> 1
// outputs 1
By themselves, numbers aren't very useful if we want to create more complex programs, like the tip calculator we will create in this section.
Think about it, when we meet new people, we often ask for basic information about them that help us get to know them. Two questions we might ask are: "what's your name?" and "what's your phone number?"
We can write a program that does this for us in a few short lines:
print("Hello. What is your name?")
print("C. Ronaldo")
If you typed this and ran it in the Sonar REPL or a .sl file, you would get the following output:
// C. Ronaldo
The print
function is used to display the output of our programs on the computer screen.
>> print(1)
// outputs 1
>> print("SonarLang")
// outputs SonarLang
>> print("Hello World")
// outputs Hello World
What does this have to do with strings?
Well, strings are just the programmers' word for "text" and text is what we've used in these programs.
"Hello. What is your name?" is text.
"C. Ronaldo" is text.
"SonarLang" is text.
The easiest way to tell if a data type is a text (or a string, to use the proper term) in Sonar is to check whether it begins and ends with double quotes. If it does, you can bet it is a string.
Usefulness of Strings
Strings and numbers are the most-useful data types in most programming languages, Sonar included. With just these two, many large and complex programs can be written.
Strings are usually used to represent normal text in everyday language use. Take the example below:
>> print("I am a girl")
// outputs I am a girl
>> print("I love ice cream!")
// outputs I love ice cream!
By combining strings and numbers together, we can write a program that calculates how much to tip after a meal at a restaurant:
print("Welcome to PizzaCircle")
let cost = 100 // e.g $100
let tip_percentage = 10/100 // 10%
// now we will make the computer calculate the appropriate tip for us
// the tip will equal 10 percent of the cost of our meal
let tip = tip_percentage * cost
print("Your tip is: ")
print(tip)
In Sonar, blank lines (such as at line 2, 5, and 9) don't matter so we use them to make our programs easier to read by other people (and ourselves!)
String Operators
Unlike many languages, Sonar allows you to use both mathematical and logic operators like "+", "-" and ">" with strings as well as numbers (for numbers, see Addition and Subtraction of Numbers).
Plus (+)
Plus (+) is used to add strings together. Sometimes, in our programs, we will need to make longer sentences by combining words and phrases, and the + symbol can be used for just that!
print("Christopher" + " " + "Columbus")
// outputs Christopher Columbus
The process of adding two or more strings together to form longer strings is called concatenation. You might here one or two programmers talk about needing to "concatenate these strings" or "concatenate those strings". They mean that they need to add some strings together to create a longer string.
Minus (-)
Minus (-) is used to remove all occurrences of a smaller string within a larger one. In this case, the smaller string is called the substring.
sub is Latin for "below"; you can remember the meaning of substring by remembering it means "lower string".
For example, imagine you need to remove all occurrences of the word "the" in a paragraph in a book, you could do this very easily like so:
let paragraph = "There will be no foolish wand-waving or silly incantations in this class. As such, I don't expect many of you to appreciate the subtle science and exact art that is potion-making. However, for those select few who possess the predisposition, I can teach you how to bewitch the mind and ensnare the senses. I can tell you how to bottle fame, brew glory, and even put a stopper in death. Then again, maybe some of you have come to Hogwarts in possession of abilities so formidable that you feel confident enough to not pay attention!"
// this paragraph, an excerpt from the Harry Potter books by J.K Rowling, contains an awful lot of "you"s
// we can remove them all by "subtracting" you (the substring) from the paragraph
let new_paragraph = paragraph - "you"
print(new_paragraph)
// outputs There will be no foolish wand-waving or silly incantations in this class. As such, I don't expect many of to appreciate the subtle science and exact art that is potion-making. However, for those select few who possess the predisposition, I can teach how to bewitch the mind and ensnare the senses. I can tell how to bottle fame, brew glory, and even put a stopper in death. Then again, maybe some of have come to Hogwarts in possession of abilities so formidable that feel confident enough to not pay attention!
Logic Operators
Logic operators (also called logical connectives) are special symbols in Mathematics and Computer Programming that can be used to compare data. The result of the use of a logical operator is always TRUE or FALSE.
Examples of logical operators are:
Greater-than (>)
Less-than (<)
Greater-or-equal (>=)
Less-or-equal (<=)
And an example of a situation where one might use these is:
2 is greater than 1 (TRUE, because 2 is really greater than 1)
1 is greater than 3 (FALSE, because 1 is really smaller/less than 3)
4 is greater than, or equal to, 4 (TRUE, because 4 is equal to 4)
Converting these to Sonar code is incredibly easy:
>> print(2 > 1)
// outputs true
>> print(1 > 3)
// outputs false
>> print(4 >= 4)
// outputs true
What if we need to compare strings? Well, in Sonar comparing strings is just as easy. The rules are a bit different than for numbers, but you can learn them pretty quickly.
>> print("A" > "B")
// outputs false because A comes first in the alphabet
>> print("B" > "A")
// outputs true
>> print("a" > "A")
// outputs true because, in Sonar, lowercase letters come before uppercase letters
>> print("a" > "b")
// outputs false because, although "a" and "b" are lowercase, "a" comes before "b" in the alphabet
In general, the rules that guide string comparison in Sonar can be written like this:
All lowercase letters come before all uppercase letters
Letters that come first in the alphabet are smaller (or less) than letters that come later
Equality of Strings
Consider two strings:
"ABC"
"ABC"
Would you say that these strings are equal?
If you answered "yes!", you would be perfectly correct.
In Sonar, we can compare two strings for equality by using the equality operator (==). The equality operator checks whether two values (strings or numbers) are equal.
>> print(1 == 1)
// outputs true
>> print(1 == 2)
// outputs false
>> print("abc" == "abc")
// outputs true
>> print("abc" == "abcd")
// outputs false
The opposite of the equality operator is the inequality operator (!=).
The inequality operator reads like "is not equal to". A few examples will help make this clear:
>> print(1 != 1)
// outputs false, because 1 is really equal to 1
>> print(1 != 2)
// outputs true, because 1 is not equal to 2
>> print("abc123" != "abc123")
// outputs false, because the two strings are the same
>> print("abc" != "abcd")
// outputs true, because the two strings aren't the same
Emojis in Sonar
In Sonar, emojis are strings too.
>> print("😃")
// outputs 😃
>> print("I'm feeling lucky! 😍")
// outputs I'm feeling lucky! 😍
String Indexing
Sometimes we might need to retrieve specific characters in a string. The process of doing this is called string indexing.
Sonar provides first-class support for indexing strings via the index operator ([]).
A good way to think of strings is like a row of characters (i.e letters, numbers and symbols). Each character in a string has a "position" and this position can be used to retrieve the character at that position.
>> let name = "Santa"
>> name[1]
// outputs a
>> name[2]
// outputs n
>> name[3]
// outputs t
Unlike humans, computers start counting from 0 so the first character in a string always has a position of 0.
>> name[0]
// outputs S
Last updated