Strings
Last updated
Last updated
In the previous section , we learned that the simplest Sonar program is really just a number.
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:
If you typed this and ran it in the Sonar REPL or a .sl file, you would get the following output:
The print
function is used to display the output of our programs on the computer screen.
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.
Strings are usually used to represent normal text in everyday language use. Take the example below:
By combining strings and numbers together, we can write a program that calculates how much to tip after a meal at a restaurant:
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!)
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!
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 (-) 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:
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:
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.
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
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.
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:
In Sonar, emojis are strings too.
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.
Unlike humans, computers start counting from 0 so the first character in a string always has a position of 0.
Strings and are the most-useful data types in most programming languages, Sonar included. With just these two, many large and complex programs can be written.
Unlike many languages, Sonar allows you to use both mathematical and logic operators like "+", "-" and ">" with strings as well as numbers (for numbers, see ).