Welcome back to learn to code in JavaScript in this lecture, we're going to talk about strings and
get a little bit more of a formal introduction.
So we've seen strings already and we've seen like Hello World in quotes and that sort of thing.
But let's talk in a little more detail.
So strings contain a list of UTF 16 characters, which is a Unicode character set.
And all that means is that it takes up two bytes or maybe four bytes depending.
And on the character set contains ABCDE.
It contains one, two, three.
It contains assigned pound sign.
It contains Chinese characters.
It contains Arabic characters.
It contains different wingdings and things like that.
And each character has a numeric representation, so every character in a string is represented by a
number.
So if you were in such a such a frame of mind, you could do some sort of arithmetic with those numbers
to come up with different strings.
So strings also support a variety of methods.
They can operate on the string, produced new strings or give results about strings and information.
Some of these methods are static, which means that you use them by saying string dot something.
So an example, string dot from CHAKO.
Now, most of these are not very useful.
So what this is saying is, given the character codes of 65, 66, 67, give me a string.
Not that useful in most cases.
So most useful methods are what they call instance methods.
And this is where you provide a function on the string itself.
So something like you have a string hello world.
You say DOT index, which is one of the instance methods in which you're looking for is a substring
that says world in that's going to be there and that's going to evaluate to six because strings are
zero based.
So H is that position zero G as it is a position one, the first Elorza position to second at three
O at four, the space at five and the W and world is at six.
There's one really useful instant property, which is length to say how long a string is, so you can
say something like Hello World that length and of course hello, world in quotes can be replaced by
a variable.
So that evaluates to 11.
So as I said, it could be a string literal or it could be a variable like my string that like.
And then we have something called string templates, so a string template has this little funny back
type character, which on your average Windows keyboard is above the tab next to the one.
So you put the back to character and it's like another type of quote.
But what you can do is put in strings into the template.
So we have a variable called name and we and we use this dollar curly brace to differentiate it so that
it replaces that spot with what's the value of the variable name.
All right.
So let's look at some selected methods that will do some examples of.
So we have a concat method which concatenate strings, and so if we have a string that has hello and
another string that has world, we can say astar concat, a space comma string to.
So we end up with Helo's based world.
Now there's another little concatenation device, which is the plus side, and you can see it right
here is that the plus sign can also concatenate strings.
So I can say the string is plus some other string and we'll concatenate them together.
And a third way to keep Cartney strings is to use that template string template with a little back tick
quotation mark that we talked about just a second ago.
And then we have some pairs of things like, for example, have starts with and ends with, so this
returns true if the string either starts with the string or ends with the string.
So in this case, in the example it's saying, I have a constant string equals Hello world, and then
I'm going to say in the council that, Lord, does the string end with world.
If it does, it's true.
It starts with just looks at the beginning instead of the end.
And then we have a method called includes.
And in this case, we're just checking to see if the substring is included in the mainstream, in the
includes, you can also specify a starting position.
Index of and last index is going to give us the index of a substring, so remember, the index is starting
at zero.
And so in this case, the example, it's going to find the index of world.
Now, if world was in the string of multiple times, it would only find the first one last index up,
finds the last one.
And in this case, you can also specify a position to start within the string.
So if you had 10 worlds in the string, you can say, find me the first one and start after that position
and finally the next one and so on.
Pad starting pattern, this is really handy for formatting things, it says go ahead and pad a string
with a certain character to get it up to a certain length.
So pads start in this example, it's going to pad hello world with asterisks.
So that is a 24 character length string.
So sometimes you do it with spaces so you can have things formatted right, justified or not justified.
More method's repeat repeats the string, so hello, world, here it is, repeat five times, such as
say hello world, hello world whole world five times.
Replace and replace all depending on which one you pick.
It's going to either replace the first occurrence of a substring or replace all of the occurrences.
So in this example, string that replace little old with Bego, it's going to just replace the first
one.
Slice, if you think about it, that's what it's doing, it's slicing a substring from the original
string.
When I say slice three, it's going to slice off the ACL and leave the Yellow Space or Helda as a substring
split splits a string on a separator, and often you have a comma separated string or something like
that.
But in this case, I've just got HTC, that, et cetera, world.
And when I do a split, it creates an array of the characters split on that string.
And we'll look at this more and more.
We're talking about arrays.
Substring pulsated substring from the original string, so starting in a position for a certain length,
get a substring out of that original string.
To strength, this is Maxim, kind of not useful in the string area, but all objects have a two string
and we'll talk about that more.
So in this example, I'm converting the number one, two, three to a string.
To lower case and to upper case.
Upper cases are lower cases, the entire string.
From start to amend or trim.
Will trim white space from the start or the end or both ends of the string, depending on which one
you choose.
So in the next election, let's look at some examples of using some of these methods and the length
and a few of the operators on strength's.