How to Recognize Strings in JavaScript and Other Languages

show more Strings in JavaScript and many other languages are smart and are built to help you. In this tutorial, you'll learn the ways you can recognize the patterns in strings and use them to write stronger, more efficient code. Simon also highlights a few common mistakes and ways to avoid them. show less
please wait ...

Finding patterns in strings

A little earlier in the course I talked about strings in JavaScript and in a lot of other languages being smart, being able to tell us information about themselves, and even having behavior. An example of this would be if I created a variable called phrase and gave it this simple value. I can then use the name of the variable, followed with a dot, and then I can access certain built-in information about it. So I am using this .length, what's called a global property, to access this fact that it's 24 characters long, and we can pop that up in an alert box.

Well, it goes a little deeper than this. If I'm working with a string in JavaScript that also has behavior and we can use the same dot operator to get to that. Strings have what are called methods and these are like functions. We've already seen functions. We call them with parentheses, but methods are like functions that belong to a string. So again, if we create a variable called phrase, what I can do is use a phrase., and then some built-in methods like toUpperCase, which I'm calling like a function using the parentheses after it.

Now because I have this in an alert box what we are going to get is that string converted to uppercase. Now I am not going to dive into every behavior that strings have. These change across different languages, but you will find in most of them strings are quite powerful and they have various built-in behaviors like converting to uppercase or converting to lowercase or allowing us to find certain pieces within them. Now a couple of things to understand about strings, where they do become a bit more complex than numbers, is because we can obviously have strings using different cases.

If I create two variables, one containing the word Hello with an uppercase H and the other containing the word hello with a lowercase h, and then I want to see if they are equal to each other, well, they won't be, because uppercase H and lowercase H are not equal. But perhaps what I want to do is a case insensitive comparison. I don't really care whether it's the uppercase or lowercase. I just want to know if the same word is in them. Well, the fact that I can use these variables and convert them means that what I could do is something that might look a little annoying here, but I'm asking if the str1 converted toLowerCase is equal to str2, converted toLowerCase, then I can ask, are they actually equal? And this is a great ability of being able to ask strings to do things for us.

One step further is we can find certain words that exist inside of strings. This is very, very common to want to do. So we create a variable called phrase and then what I am using is what's called the indexOf method of the string. I am using that same dot operator and then indexOf. It's an lowercase i and an uppercase O, and I am passing in another string. In this case I am passing in the word groovy, and I want to find out what is the index of groovy? Meaning, if it exists, where does it begin in the string? Now what's going to happen is if that word exists in the first string, it's going to return the position and that's zero based.

So the first letter of the string, which is the uppercase W, will be positioned 0. Groovy will start at position 10. If that word is not found in the string, this will return -1. So if for example, I wanted to find out, if a certain phrase or word did not occur, I could call phrase. indexOf, passing in a DDDD. We know that does not occur. And if the result of that was -1, I could pop up an alert box that says, no, that word does not occur in the string.

And of course, there are dozens of ways that you could combine these different kinds of operations to find out if a phrase was in a string or if it was not in a string, but this is one example of doing this. Now again, in a course like this, I'm not really expecting you to try and memorize this syntax. What I am wanting you to do is understand that this is possible. That understand that, yes, I can find one string and I can see if it exists in another one or I can see if it does not exist in another one. You can always look the syntax up, you can always find cookbooks and examples online, but you need to know that this stuff is possible.

There is also a method called lastIndexOf. Whereas .indexOf will give us the first occurrence of the word in the larger string, .lastIndexOf will give us the last position of that in the string. So if we are working with larger amounts of text that might be useful. We can also start breaking a string apart. There are several methods for this. One of the ones I like is called slice. So we create a variable called phrase, just give it some basic text.

And what I'm going to do is create a new variable called segment based on part of phrase, and I am going to use phrase.slice, and slice expects two arguments. In this case I am some passing in 6,11. What does that mean? What we are going to do is the first number is the position that we are going to start at in the phrase, and it starts from 0, so 0123456, 6 is the o of another. Then 11 would be the position we are going to stop by.

So we are basically going to get 6, 7, 8, 9, 10. We will get those letters and we will store that result in the second variable, and that's how we could pull a section out of another string. There are also methods like .substring() and .substr() that do have similar ways of dealing with this, but they give us a couple of other options. I am a fan of slice, but you can look at the others if you're interested. More than ever what you should be taking from this is not necessarily trying to remember the syntax or remember the exact format, but just understanding the JavaScript, and JavaScript is a pretty lightweight language, has a whole lot of built-in functionality for manipulating strings, for getting to them, for understanding where the other words exist inside them.

You'll find that across all modern programming languages and it can come in very handy. One final thing to explore is the idea of comparing strings, not for equality, but for greater than and less than. We can do this. Let's say we've got a couple of basic strings here, aardvark and beluga. Then I write an if statement. If (str1 < str2). What it's going to do is start comparing the first letters. In this case, really asking is a less than b, almost like a phonebook comparison.

In this case, yes, absolutely! This would be true. However, the fact of adding in the uppercase and lowercase parts of it can make things a bit more confusing, in not necessarily the way you expect. So here in the second example, I have aardvark with a lowercase a, and Beluga with an uppercase B. Again, I ask if there is a comparison? Is str1 < str2? Most people would think, well, yes it is, but here is the problem. This would actually be regarded as false, and that's because as far as the internal structure of how we encode characters in a programming language, the uppercase letters are regarded as less than the lowercase letters.

So uppercase B is regarded as coming before a lowercase a. So this will be false. A technique that I might use to ignore this would be once again using the ability of the string to be converted and calling toLowercase on both of these, before I actually compared the two. So all you can see, there are a few gotchas when dealing with strings and some of the behaviors are not necessarily intuitive when you're new to this. You can also see that strings have a lot of built-in behavior that allows you different ways of dealing with this.

Finding patterns in strings
Video duration: 8m 3s 4h 47m Beginner



Developer Web
please wait ...