Strings

Also known as text.

They “string together” a series of individual characters.

Strings are yet another kind of value

So we can ask our usual questons

What values can be represented?

Basically any text (letters, numbers, etc.)

How do we write strings?

Two ways to write literal strings.

"foo"

'foo'

Note that the quotation marks are not part of the string.

Both "foo" and 'foo' are three characters long.

They also represent the same value despite being written differently.

The + operator works on strings

Also known as “concatenation”

» 'foo' + 'bar'
'foobar'

⚠️ Watch out though ⚠️

When asked to add different types of values, and one of them is a string, Javascript will convert the other one to a string and then concatenate them.

» 1 + '0'
'10'

But the other arithmetic operators will convert strings to numbers in mixed expressions.

» 1 * '0'
0

Taking apart strings

[] operator

The “index” operator.

Allows us to select a single character out of a string.

Unlike operators like + and *, it doesn’t go between its operands.

Also its operands are not the same type.

string[index]

The string operand is a string.

The index operand is a number.

Expression evaluates to a one-character string.

Note: indexes start at 0

The index of the first character is 0.

This is called a zero-based index.

An index tells you how many characters are ahead of the character at that index.

» 'abc'[0]
'a'

» 'abc'[1]
'b'

» 'abc'[2]
'c'

The length property

Some values in Javascript have “properties” that hold other values.

Strings have a length property that says how long they are.

Properties are accessed with a . after a value followed by the name of the property.

» 'foobar'.length
6

Valid indices for a string s

If the index of the first character is 0 what is the last valid index?

s.length - 1

Methods preview

Some values in Javascript have special properties called “methods”.

Methods are bits of functionality, similar to functions, but attached to the value.

value.methodName(arguments)

slice method

Sometimes we want to extract a bigger chunk of a string than a single character.

Strings have a slice method for this.

Extracts a part of the string

From a starting index (inclusive) to an end index (exclusive)

s.slice(start index, end index)

or

s.slice(start index)

Second argument is implicitly s.length.

Negative arguments to slice

A negative index to slice is treated as if it was subtracted from the string’s length.

s.slice(-3)

is equivalent to

s.slice(s.length - 3)

s = 'foobar'

» s.slice(0)
'foobar'

» s.slice(1)
'oobar'

» s.slice(s.length - 1)
'r'

» s.slice(-1)
'r'

» s.slice(s.length)
''

» s.slice(0, 3)
'foo'

» s.slice(3)
'bar'

» s.slice(3, s.length)
'bar'

» s.slice(3, -1)
'ba'

Finding strings within strings

The indexOf method can find where one string occurs in another.

s.indexOf("o")1

s.indexOf("oba")2

Don’t confuse index operator [] and indexOf

Index operator [] takes an index (a number) and returns a one-character string.

s[3]"b"

indexOf takes a string argument and returns an index (a number)

s.indexOf("b")3

Some other useful String methods

» 'foo'.toUpperCase()
'FOO'

» 'FOO'.toLowerCase()
'foo'

f = 'foo'

f[0].toUpperCase() + f.slice(1)
'Foo'

Try it yourself

String expressions