What is a string?
Video outline
Definition
A string is a data type used in programming, that is used to represent text rather than numbers. A string is a sequence of characters and can contain letters, numbers, symbols and even spaces. It must be enclosed in quotation marks for it to be recognized as a string.
For example, the word “liquid” and the phrase “What is liquid? It’s a template language.” are both strings. Even the sequence of characters “98OkJJ$%janUJ£” is considered a string.
Practical example
Let’s go over an example of how we could use this. Say we want to show a certain sentence only when the companytype is a limited. First, we create a variable named companytype and assign the string “limited” to it.
{% assign companytype = "limited" %}
Later in our code we are going to check what companytype it is and only show the sentence if the companytype is a limited. We can do that with this piece of code: if companytype equals limited than the Sentence for limited companies will be printed in the template.
{% if companytype == "limited" %}
Sentence for limited companies
{% endif %}
Points of attention
Strings may sometimes look similar to other data types. But it’s important to remember that even if a number looks like a number, or a boolean looks like a boolean, it’s good to check that it’s not a string or else you won’t be able to use it correctly!
Take the following string: '20'. The string '20' is different from the numerical value 20. While they appear to be the same, a computer would see the first as two characters: '2' and '0', while the second as the numerical value of 20.
Also keep in mind that this is case sensitive. Should we start the string assigned to the variable "companytype" with an uppercase letter L, the two strings that are being compared in our example won’t match anymore and the sentence will not be printed.
Updated over 3 years ago