1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Strings and the StringBuilder Class in Java

StringBuilder

Contents

keyboard_tab
Strings and the StingBuilder Class
1
Course Overview
PREVIEW1m 4s
2
String Methods Part 1
PREVIEW12m 55s
3

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
47m
Students
92
Ratings
5/5
starstarstarstarstar
Description

This course delves into the String class and looks at the functionality it provides through a host of methods. We will also discuss how string objects are immutable, which means the object itself can't be modified once they've been created.

Learning Objectives

  • Learn how the StringBuilder class differs from the string class

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

In the previous couple lectures, we learned some extremely popular methods of the String class. I've mentioned that the string objects themselves cannot be modified. Even though we can change the string that a reference variable is referencing, in other words, changing the address that it holds to point to another different string object, you've noticed that the methods we've used often return a new String object with modifications made but do not affect the original string object. Because the string class does not allow the object to be modified, we say that strings are immutable. If we have frequent modifications to perform, the only option we have with the string class is to create a new object with the modification made possibly over and over again. This can be very inefficient. A question might be asked: Is there a class that is like string in that it contains textual data but is mutable?

That is it can be mutated or modified. The answer is yes. And that class is called StringBuilder. StringBuilder objects don't work as fluidly as strings. For example, you can't just set a StringBuilder object equal to a string literal with double quotes, using just the assignment operator, that is the equal symbol. However, as indicated, we get an efficiency benefit for situations where the text we wish to represent must be modified in place without recreating new strings all the time. Also, StringBuilder has many of the same methods as the string class, such as charAt, indexOf, and length, as well as some that the String class doesn't have largely due to the mutability differences between those two classes. 

Let's do an example to see a few of the methods that StringBuilder has that the regular string class doesn't. So, let's create a new Java Class named StringBuilderFun. Right click 'New', 'Java Class', StringBuilderFun. And of course, let's write some code. I like bringing the class down a little bit there, and we'll put public static void main(string[]args)  and again, my little optional comment I put there, let's create a StringBuilder. So, StringBuilder sb = new StringBuilder,  and I'm going to actually initialize it with my name. So, you can't actually pass a string to the StringBuilder constructor;  it's one of the constructors available that will take the string. So, the next thing we're going to look at is the append method. So, sb.append, and I'm going to put (" is awesome"). So, the append method concatenates the data to the end of the text data held by the StringBuilder. And of course, let's print this out. Right there. I'm just going to put sb.

The print line method knows how to print out the sb, the StringBuilder, and work with references like that. So, it knows that we're supposed to print out the contents. That's the string. The StringBuilder actually has some of this built into itself, so that it knows to return the string that the print line method needs. So, the next thing we'll look at is the insert method. So, sb.insert. And we're going to say 5 and then Philip and a space. And the insert method places data inside a string, or StringBuilder in this case, and has given a start index and the data to insert as the two parameters. And then of course, we'll print it again. System.out.println, we'll put print (sb). And you notice, I inserted Philip and a space at index 5. So, if we look at my name, we have 0, 1, 2, 3, 4, and I'm inserting Phillips starting at this index. So, we'll actually push Baugh over, and then I put the space there. 

So, it will be a space between my middle name, Philip and the name Baugh. All right. The next thing we'll do is this. So, sb.replace,  and I'm going to put (22, 29, "amazing"). And the replace method is another method that you can call on StringBuilder objects that you can't call on string objects. This method takes three parameters. I got to change that to amazing. So, it takes three parameters: The starting index, the ending index, and the string data to replace the content with. The ending index is an upper bound but isn't included in the characters being replaced so it's excluded. So, we're replacing awesome with amazing. If you were to think about where is awesome exists in the string that we've built up to this point and StringBuilder that we've built if you think of it that way, and then where Phillip is, you'll find that 22 through 28 inclusive or 29 exclusive is where the word amazing should go. 

So, it's replacing the word awesome. And of course let's print it. Print line. So, let's say that I decided to remove my middle name. The delete method has two parameters, like the first two parameters of replace the beginning index and the ending excluded index. So, to remove my middle name, I would say sb.delete, starting at character 5, and going up to but excluding rather index 13, and then of course, print it out again. So, let's see if we get what we expect here.

So, I'm going to right click 'StringBuilderFun' and run it. And we get John Baugh is awesome. John Philip Baugh is awesome. John Phillip Baugh is amazing because remember we did a replace up here. We replaced awesome with amazing. And then John Baugh is amazing because we removed my middle name and the extra space. Pretty cool. So, it was this before the delete and then 0, 1, 2, 3, 4, 5 upto 13. So, 5, 6, 7, 8, 9, 10, 11, 12, 13 but not including it. Awesome work. So, we've seen the append, insert, replace, and delete methods which are some of the awesome methods that StringBuilder offers but the string class doesn't. Before moving on, I have a challenge for you. Using the same file, StringBuilderFun, I'd like you to start near the end of the file after I deleted my middle name. What I'd like you to do is replace my first name with my formal title, Dr., but use the abbreviation of course, "Dr.". 

Also, don't forget to print it out after you make the replacement. So, pause the video and give this one your best shot. Come back when you're done or if you need some help. How did that go for you? Were you able to replace my name with my title? Let's do this together. So, right after we made this modification and deletion right here, I'm going to put sb.replace, and it starts at 0 goes up to 4. So, that's J-O-H-N. That's 0, 1, 2, 3 but not including 4. And then replace that with Dr., and of course, print it out. Awesome. Let's run it. Right-click 'Run' and there you go, Dr. Baugh is amazing. I am pretty cool. You got to admit. But nice work everyone. 

StringBuilder gives us an efficient way to work with textual data, and it's sometimes the appropriate choice, especially when textual data needs a lot of editing and modification. In the next lecture, we'll take a look at the section project, a name permutations project. I'll see you there.

 

About the Author
Students
796
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics