I am string!
'I am a string!'
"I am a string as well, with double quotes!"
' single quotes, or
], objects use
Working with strings
Strings like numbers also have operators that we can use. For example, if we had
'Hi, my name is ' and
'Edwin', how would we combine these into a single string? Go to the console and type in the following and hit enter:
'Hi, my name is ' + 'Edwin'
The output value from the console is now just one string;
'Hi, my name is Edwin'. This becomes a handy way of combining messages together, and is often referred to as string concatenation. Strings are also a series of characters so we can find out which letter is at each point like this. Take note of the pattern we write String.charAt(position)
'Cat'.charAt(0) // Outputs "C" 'Cat'.charAt(1) // Outputs "a" 'Cat'.charAt(2) // Outputs "t" 'Cat'.charAt(3) // Outputs ""
0 is the starting point in our series of letters. It may be weird to access the first character at 0, but this is a fairly common convention across all programming languages, and it has its benefits. We can also access characters using a different syntax:
'Cat' // Outputs "C" 'Cat' // Outputs "a" 'Cat' // Outputs "t" 'Cat' // Outputs undefined
This is another way to find what character is at a certain position in the string. This time when we go to an empty position we get undefined, instead of an empty string. This is the main difference between these two access methods.
As seen above we see
"" as one of the values returned. This is an empty string, same for
''.length we get
( '' + 'Hi!' ).length // Outputs 3, first we add together the empty string and Hi! to get a new string 'Hi!' which has three characters in it.
Strings are a special kind of array
[ 1, 2, 3 ]. Arrays have a similar lookup syntax to the string we used earlier. In fact the string character lookup is the array lookup syntax. Try the following in your console.
[ 'C', 'a', 't' ] // Outputs "C" same as 'Cat' [ 'C', 'a', 't' ] // Outputs "a" same as 'Cat' [ 'C', 'a', 't' ] // Outputs "t" same as 'Cat' [ 'C', 'a', 't' ] // Outputs undefined same as 'Cat'
So strings are a much more human friendly way to interact with text in a programming language. We don’t have the brackets, commas etc. Although arrays and strings share similarities, they are definitely their own thing, and are used for different purposes. We can go back and forth between string and array form using the
'Cat'.split('') // Outputs [ 'C', 'a', 't' ] [ 'C', 'a', 't' ].join('') // Outputs 'Cat'
split, a string method, to separate the string into an array. We use an empty string
'' as the split piece, causing each letter to become its own part of the array.
We then work backwards going from the array and calling join, an array method, to crunch the array back into a string. We can join the array using a different string as well like this:
[ 'I', 'am', 'happy!' ].join( ' ' ) // Outputs "I am happy!" 'AATCCGCTAG'.split('').join('G') // Outputs "AGAGTGCGCGGGCGTGAGG"
We see in the first example that each string part gets joined together with a space. making a sentence. In the next example we see something much more interesting. A series of nucleotide notations are being split into an array, and we are joining back together with in a G between each letter; in effect editing the genome sequence! 😲 Fiddling around with basic strings can lead to astounding real world applications.
Things to watch out for
Strings are usually pretty easy to work with until you get to one major problem, what if we have a string like this:
We get our first syntax error:
\ escapes the
'. Escaping gets real confusing really quickly, what if we wanted to have the string: