Working with strings
Video: Working with stringsSo as we've seen, to create variables that are string values, you simply surround your word or your phrase with double quotes. Single quotes work as well. What you can't do is mix them up, can't have an opening single quote and a closing double quote. That can lead to issues if you need that punctuation actually inside your string. So for example, the phrase "Don't mix your quotes inside single quotes" is not going to work because the second single quote there will be perceived as closing the string. We could of course put that inside the double quotes because we're not now mixing them.
Viewers: in countries Watching now:
- Creating variables, functions, and loops
- Writing conditional code
- Sending messages to the console
- Working with different variable types and objects
- Creating and changing DOM objects
- Event handling
- Working with timers
- Building smarter forms
- Using regular expressions
Working with strings
So as we've seen, to create variables that are string values, you simply surround your word or your phrase with double quotes. Single quotes work as well. What you can't do is mix them up, can't have an opening single quote and a closing double quote. That can lead to issues if you need that punctuation actually inside your string. So for example, the phrase "Don't mix your quotes inside single quotes" is not going to work because the second single quote there will be perceived as closing the string. We could of course put that inside the double quotes because we're not now mixing them.
Now, strings can be treated as objects, and what that means is they have information we can ask of them. They have properties that we can look at. If I create a variable with this simple phrase in it then I can use the name of the variable, in this case phrase, and a dot, and I can access the length property, find out how long is the string? How many characters is the string? In this case, it's 24. That's because, in a sense, strings can be treated as arrays of characters, and the same way we have a .length property of an array, we have a .length property of a string.
Well, not only do we have properties, we also have methods. We have things that these string variables can do if we ask them. So again, creating a variable called phrase, I use that variable name phrase and a dot, and I can call methods like functions of the object. So phrase.toUpperCase, which will return, not surprisingly, the entire string all uppercased. I also have.toLowerCase. We have methods like Split. This will allow us to take a longer phrase and pull it apart.
It will actually create an array from our string. So I use the variable named phrase.split, pass it a parameter--I'll talk about that in a second--and that will return an array into the variable called words, an array of words. Now, what we're passing into the Split method is a space, just using the double quotes, space, double quotes. That means I want to split this string apart at the space, because it's not always a space. Say I have some comma-separated values.
I might call split and pass double quote, comma, double quote, create an array and split them apart on the commas. So this creates us an array that is 5 elements long, 0, 1, 2, 3, and 4, and notice that the last word, phrase, also includes the period at the end of it. But Split can be a very useful method, as can indexOf. This allows us to find out if a particular term or word appears anywhere in the string. So again, with a simple string variable, I can call indexOf, passing in the term that I'm looking for, which of course has to be another string here, and it will return the position of which that string is found, if it's found it all.
So it's going to scan through the original string, and in this case it'll say, "Oh, I find the word 'groovy' appears beginning at position 10," and it will store that result in a new variable called position. If the term that you're searching for is found right at the start, it'll return 0. Now, the question is well, what happens if that is not found at all? Sometimes that's just as important, if we need to know this term is not found. Well, what that'll do is return -1. So I could say put that in an if statement, if phrase.indexOf, pass in DDDD is equal to, again using the double equals, which will be exactly the same as triple equals here, -1, then we can do a message saying, That word does not occur.
We called indexOf. We passed in the term. It said -1. It doesn't exist there. Now, indexOf finds the first occurrence of that term in the phrase. If it could occur multiple times and we want the last one, we can also ask for .lastIndexOf. Next, the same way that we have Split to split a string apart into an array, we also have the method called Slice; to allow us to slice out a particular piece of that string. So I am going to begin with a simple phrase and I'm going to store that in a variable called Phrase.
And what I can do is now call phrase. slice, and pass in two numbers, in this case 6,11, and that is the starting position and the ending position. So what this will do is count six positions into that phrase variable, again, treating the string as an array of characters so we starting at position 0, and it will grab from position 6, up to, though not including position 11, because we are grabbing five characters, from 6 to 11. And it will return that value into the new variable called segment.
Just the number of characters to return. And you can use whichever one makes most sense to you. And finally, a couple of things to be aware of with string comparison. If we have two strings that are exactly the same thing, then we can use the double equals or the triple equals to match them, but our string comparison here is case sensitive, so right now if I have Hello with an uppercase H and hello with a lowercase h, they will not be considered equal to each other. If you're worried about that kind of thing, the easiest thing would be to call toLowerCase, or alternatively, toUpperCase on both of them, if you were at all unsure that there might be a casing difference in there, and then I can check them against each other.
Now something you might also be concerned about is the sort order. What happens if I use the less than or greater than? Let's say we start off with two strings: aardvark and beluga. If I want to ask, is String 1 less than String 2, this works just fine. It's going to compare beginning with the first letters, and it's going to say yes, A is less than B, so that will return true. Well how about the case where I, for example, use an uppercase B? And I'm wondering is aardvark less than Beluga with an uppercase B? Well, in fact, this case will return false.
You want to know where it is and find it when you need it.