Java Fundamentals Backwards String & StringBuilder
Transcript from the "Backwards String & StringBuilder" Lesson
>> So now we're gonna write a backwards string. Write a method that prints a given string backwards. For example, if given pots, it prints stop. All right, so I'm gonna comment this one, and we're gonna make another call. This one will be called reverseString, we can give it some string.
[00:00:30] We haven't written Hello World yet, let's do it. All right, so we'll say, public static void reverseString. This will take a string. Okay, So here, I need to loop through this string. Now, a string, again, is a sequence of characters. So really, you can think of a string, I hate to say this, [LAUGH] cuz someone's gonna say, well, technically.
[00:01:17] All right, so you can think of a string as an array of chars. Remember we talked about this individual things, right? So it's a sequence, and you can access elements of an array as if it wasn't, I mean, elements of a string as if it were an array, all right?
[00:01:39] So I can say, what's the third element of the string? And it'll give me the character with an index of two cuz that would be the third, right? Yes, all right, so we're going to use a for loop. I'm gonna use an old school one cuz I wanna show you the i and going inside of the array of characters.
[00:02:08] So this time, we're not gonna start at 0 cuz we need to reverse the string. So we're gonna start at the back of the string, okay? So in order to do that, we can say, text.length. This will give the length of the string. How many characters are in this string?
[00:02:30] That's where I wanna start. Now, Indices start at 0. So if I say .length, if the word was Hello World, how many characters is that? 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 is what I'm counting. Length would be 11. Is there an array index of 11 for this?
[00:03:01] I see some head nodding no, shaking no.
>> You need plus 1 to find the boundary, it's just outside of the array, correct?
>> Okay, so 11 would be the length. That means what is the last index?
>> 10 is the last index. So I wanna do length()- 1, all right?
[00:03:23] And that'll give me that very last element, all right? And then I wanna say, you wanna keep doing this, wow, i is greater than or equal to 0. Keep looping cuz we haven't gotten to the first element yet. And then I wanna say, not i++, i-- cuz I want to decrement.
[00:03:49] i-- is the same as i = i- 1. Okay, any questions on that loop declaration? All right, now that we're in there, we're going backwards here. Let's just print. And we can say text.. There is a convenience method on the string object called charAt that takes an integer for the index.
[00:04:30] That's why I wanted to use, This for loop with the incrementer for a number of reasons. One, I need to go backwards, two, I wanna call the charAt, which takes an int for the index, right? And that should print the string backwards, let's test it out. I have it all on different lines, I'm just gonna do a print instead of println, and boom.
>> Could we have used bracket notation to get the charAt?
>> Yeah, you could. So if I wanted to do, let me see if I can just print it like that. I might be able to just say text[i], does that work? No, wait type. Cuz yeah, yeah, it's not that.
[00:05:39] So no, you couldn't just use the brackets on it since it is a string object and not technically an array. So the charAt is the closest you can get there. All right, any other questions?
>> So this method reverses and just prints it out, but what if you wanted to, Manually or manually change the values of the array so that way they're actually reversed within the array itself if that makes sense?
>> I am so glad you asked that. So you cannot change [LAUGH] values and things within the string itself. Because as the question came up a little while ago, strings are immutable. What that means is that they cannot be modified, okay? So we did things like, we basically read the string.
[00:06:48] Yeah, when we print stuff, we append a little something to it, things like that. But as far as deleting something, a character from a string, or inserting something somewhere, you are not able to do that sort of thing with the string object. Therefore, we have a string builder class.
[00:07:09] The string builder creates a mutable version of the string that you then can manipulate however you want to.