Strings
Last updated
Last updated
Strings, as mentioned are any ordered set of characters enclosed in quotation marks.
To access a particular character in a string at a particular position, there are 3 ways:
Square bracket indexes ([]
)
The charAt
method
The at
method
Let's create a string, and use the 3 methods to compare outputs with different cases:
Usage: str[i]
where str
is the string and i
is a nonnegative integer
We can see from the above that passing in any value for i
that is not a nonnegative integer less than the length of the string results in undefined
.
charAt
methodUsage: str.charAt(i)
where str
is the string i
is a number
We can see from the above that:
passing in a negative value for i
returns the first character in the string
passing in a decimal value for i
returns the index at position floor(i)
passing in a value greater than the length for i
returns an empty string ""
at
methodUsage: str.at(i)
where str
is the string i
is a number
We can see from the above that the at
method behaves similarly to the charAt
method except for negative indices. In this case, an index of -1 corresponds to the last character in a string, and every negative number after that is counting backwards. Thus an index of -2 is the second-to-last character in the string, -3 is the third-to-last character, and so on.
We've already seen 2 string methods that help to get a character in the string at a particular position: charAt
and at
.
Let's look at some string formatting methods.
To convert a string to all uppercase, we have the toUpperCase
method. You can probably guess from this that the method to convert a string to all lowercase is toLowerCase
.
Note that the value of name
has not changed after applying the string methods to it. This is because strings are immutable, so the methods just create new strings.
To get a subsection of a string, called a substring, there are a few methods.
slice
The first is the slice
method. This takes in two arguments, a start
and end
, and returns the characters in the string from the start
index to just before the end
index:
Ommitting the end
value will return all the characters from the start
to the end of the string:
If the end
value is greater than or equal to the start
value, an empty string is returned:
If only one argument k
is passed in, and the argument is negative, then the last |k|
characters are returned:
substr
The next is the substr
method. This takes in two arguments, a start
and length
, and returns the first length
characters from the start
index inclusive.
Ommitting the length
value will return all the characters from the start
to the end of the string:
If the start
is negative, then the characters are counted backwards:
substring
The last method is the substring
method. This works like the slice
method but with one change. If end
is greater than start
, then the two are swapped:
There are a few methods to help check if one string is a substring of another. These are includes
, startsWith
and endsWith
. They are case-sensitive and their names are pretty self-explanatory.
Let's say we want a simple program that will add two numbers and output their sum in the form "x + y = sum".
Passing in multiple arguments console.log
causes them to be joined into one string with each argument separated by a space. However this method can only be used when a function is able to take in multiple arguments and knows to concatenate them into a string, like console.log
. The same code will not work with alert
(see below).
This method works similarly to string concatenation, but when substituting multiple values into a string it looks more clean and is easier to read. To form a string template, wrap the string in backticks (``) instead of quotes. Then, at the positions of the variables, use curly braces and the $
sign before the braces, and place the variable/expression between the braces. See below:
While this has the same effect as before, it looks a lot cleaner and its clearer to anyone reading the code as to what is going on. There is also less chance of operations getting confused.
Next, we'll look at arrays and some useful array methods.
Strings are provided with a whole host of functions that can operate on strings. These functions are called string methods, and W3Schools has a an of these functions. For now, we'll look at a few in detail, and how they work.
As you hopefully recall, the when applied to two strings (or to a number and a string) performs string concatenation. This results in a single string being passed in to the function to print out. The advantage of this is that string concatenation results in a string value being formed, so it can be passed in to any function, and can be assigned to a variable/constant as needed. However, if I was not careful and omitted the brackets around x + y
at the end, I would run into a problem (see below).