Transcript from the "Strings & Text Processing" Lesson
>> So this next section is about strings. Someone asked earlier if a string was a wrapper class. It is not, and it's also not a primitive data type. So a string is an object, and it represents a sequence of characters, right. The string class actually provides lots of helpful convenience methods just like those.
[00:00:24] Wrapper classes do so it does have that in common. And let's explore some of the common methods that are available. We're gonna write a bunch of little fun programs there and by fun I mean. These are the types of things that people give you in coding interviews and stuff.
[00:00:42] So not fun.
>> Get you a job.
>> Yeah, [LAUGH] But it's a great way that we can see a lot of the convenience methods that are available in string. And see how to think through some of these things, all right. So this first one is a word count, we're gonna write a method that counts the number of words in a string.
[00:01:02] And prints them individually on new lines. All right, so back in our ID. Let's go ahead and make a new package. And this section is on text in general, not just strings, but we could just call it strings or whatever. Am I in the right place? Yeah, okay, and then let's go ahead and create a new class.
[00:01:36] We'll call this one TextProcessor All right. Now we're gonna do a bunch of these little exercises. So we'll make a main method. That'll just call the methods, and we'll write a method for each one of these little exercises, okay. So the first one we're gonna do is count words.
[00:02:00] You can put whatever you want. I'm gonna put ("I love Java"). And then we'll make that method. So we'll say public static void count words. And this will take a string and now we want to do basically some tokenizing of this, right. So we can go ahead and use var words equals.
[00:02:35] And with this text, I wanna say dot and notice all of these helper methods that I have available. I can do to lowercase or to uppercase and all sorts of other things. What I wanna do is split this text. And this takes a regular expression, which I am not good at, but I know that I wanna split this.
[00:02:59] If I'm talking about how many words are in there, what would be a good way to split this? By the space, right. So let's go ahead and spli.t it on a blank space. And that way this should split this right here I should be one, love should be one, Java should be one.
[00:03:19] Now notice here I use var and this resolves to an array of strings is what it resolves to. So I'll get three strings there so in order to know how many elements are in the array which should I use? What should I use? How do I know how many elements are in this array?
>> Yes length words.length, right. So we talked about that in the array chapter. Where you can see how many elements are in an array using length. Definitely keep that in the back of your head. I always use length all the time. It's very popular, all right.
[00:04:09] So now I know the number of words there are and then the other prompt said to print them all on a new line, right. So, I need to first let them know how many numbers. So let's do that first, let's do a print statement and I wanna use something remember you may not remember.
[00:04:35] But much earlier lesson, I specified that there were a number of ways that you could append a variable in the middle of a string. So we're gonna use another more kind of advanced method to do that now. So I want to use, let's do this. Let's first declare the message, I'm gonna say it's a string.
[00:05:01] What I want to use is this string.format. So string.format is a nice method that will allow me to essentially add placeholders within a string. And then give it the variables that need to be replaced, okay. So, I wanna say, and it's just much cleaner, ("your text contains"). Now, this is where I would have did a plus, did the number of words, then another plus, I need to add the spaces.
[00:05:34] So, instead of that I'm gonna add a placeholder. In the form of %d, so this format has a couple of different percent like symbols. D stands for digit, so this is saying I'm gonna give you a digit to put right here. So your text contains %d words. It's a placeholder.
[00:05:58] Now, format then takes however many more arguments you need to give it to replace any placeholders you have. So we only have one placeholder. We would just give it that variable, so number of words. And the reason you want to use this is this just reads really cleanly versus plus sign.
[00:06:24] Did I forget the space? You know what I mean? So this is like just really clean.
>> Sorry, what exactly is regex?
>> Regular expressions, Whole nother course. [LAUGH] All right, okay, so we formatted our message. Now we can go ahead and print that message out. All right, so that tells them the number of words but then we also needed to print the words themselves.
[00:07:00] So we can use a loop, right. We have an array. We wanna print everything in the loop. I mean in everything in the array, we use a loop, and we could do the enhanced one. So we said, all right. For all of the words which is an array take each one assign it to a variable that's called word.
[00:07:25] And then go ahead and print that word out, Okay. And that is our count words. Let's run it. So this shows your text contains three words I love Java. You if you wanna test this you can change this to. However many words you want, test it out, all right.
[00:07:59] So with that, we looked at the split method, which is a method on string as an object. Notice to, there's a couple of things we did. So this is a text as a string. We use the text object to say dot split, Versus here we use the string class and say dot format.
[00:08:23] Why were we able to do this? What's the difference here? Why did one time we use the object, and the other time we use the name of the class? I'll give you a hint. For the entire day, people have been asking me, what does this mean? [LAUGH] Static, right.
[00:08:46] All right, so the split method, notice it didn't ask me for the text that it needs to split because it's an object. It's part of the object. It has the data already. So when I said text, which is an object, dot split, it already has whatever the value of this text is.
[00:09:08] And it's able to just do what it needs to do. Those is format I had to actually give it a string, and any placeholders. It has no context of any data because it is static. So there are some methods that can be static, you don't have to have any data.
[00:09:28] You don't have to know anything, there is no world, right. Just call it versus other methods like split are not static, they are part of the object. So that's what the static part means.