Contenido del Curso
Principios Básicos de Java
Principios Básicos de Java
StringBuilder
What is StringBuilder?
StringBuilder
is a utility class for manipulating and editing string objects.
Note
You will learn more about classes, how to create them, and how to work with them correctly in a separate course.
StringBuilder
has its own methods, and creating a StringBuilder
object is as straightforward as creating a String
object:
Inside the parentheses, we can also pass a string value, and our StringBuilder
variable will hold that value, for example:
Main
StringBuilder builder = new StringBuilder("c<>definity");
To print our value to the console, we need to use one of the StringBuilder
methods. It's the toString()
method, which converts the value of the StringBuilder to a String type value.
Let's look at an example:
Main
package com.example; public class Main { public static void main(String[] args) { StringBuilder builder = new StringBuilder("c<>definity"); String string = builder.toString(); System.out.println(string); } }
Note
Keep in mind that if your
StringBuilder
has a null value, it will result in aNullPointerException
(NPE) being thrown.
Main
package com.example; public class Main { public static void main(String[] args) { StringBuilder builder = new StringBuilder("c<>definity"); String string = builder.toString(); System.out.println(string); } }
Why Not Use the + Operator?
You might be wondering why we don't simply use the + operator for string manipulation. Well, performing mathematical operations with string variables is discouraged in Java. Even using == for comparison is discouraged in most cases, except when comparing with null. (Remember, we can't use methods with null values, right?)
Instead, it's better to use the equals()
method for string comparison, which we will delve into in the next chapter.
Main
package com.example; public class Main { public static void main(String[] args) { // initializing the new String with value "Hello" String hello = "Hello"; System.out.println("Our string before using an append() method: " + hello); // initializing a new StringBuilder with value of our already initialized string StringBuilder builder = new StringBuilder(hello); // using append() method, adding a string world to our StringBuilder builder = builder.append(" world!"); // creating a new String variable that has a StringBuilder value String result = builder.toString(); System.out.println("Our string after using an append() method: " + result); } }
Note
Be extremely cautious when using
StringBuilder
, and remember that aString
cannot be modified after initialization. If you encounter a situation in your program where you think your methods are not working, double-check whether you have considered this.
Swipe to begin your solution
Create a string that lists products with their prices for display on a webpage. Use StringBuilder
to construct the string.
- Add the header
"Product List:\n"
to theStringBuilder
(where\n
represents a line break). - Loop through the array using a
for-each
loop. - Get the product name from each array element.
- Get the product price from each array element.
- Append the product name and price to the
StringBuilder
in the correct format (Laptop - $999.99). - Output the content of the
StringBuilder
to the console.
Solución
solution
¡Gracias por tus comentarios!
StringBuilder
What is StringBuilder?
StringBuilder
is a utility class for manipulating and editing string objects.
Note
You will learn more about classes, how to create them, and how to work with them correctly in a separate course.
StringBuilder
has its own methods, and creating a StringBuilder
object is as straightforward as creating a String
object:
Inside the parentheses, we can also pass a string value, and our StringBuilder
variable will hold that value, for example:
Main
StringBuilder builder = new StringBuilder("c<>definity");
To print our value to the console, we need to use one of the StringBuilder
methods. It's the toString()
method, which converts the value of the StringBuilder to a String type value.
Let's look at an example:
Main
package com.example; public class Main { public static void main(String[] args) { StringBuilder builder = new StringBuilder("c<>definity"); String string = builder.toString(); System.out.println(string); } }
Note
Keep in mind that if your
StringBuilder
has a null value, it will result in aNullPointerException
(NPE) being thrown.
Main
package com.example; public class Main { public static void main(String[] args) { StringBuilder builder = new StringBuilder("c<>definity"); String string = builder.toString(); System.out.println(string); } }
Why Not Use the + Operator?
You might be wondering why we don't simply use the + operator for string manipulation. Well, performing mathematical operations with string variables is discouraged in Java. Even using == for comparison is discouraged in most cases, except when comparing with null. (Remember, we can't use methods with null values, right?)
Instead, it's better to use the equals()
method for string comparison, which we will delve into in the next chapter.
Main
package com.example; public class Main { public static void main(String[] args) { // initializing the new String with value "Hello" String hello = "Hello"; System.out.println("Our string before using an append() method: " + hello); // initializing a new StringBuilder with value of our already initialized string StringBuilder builder = new StringBuilder(hello); // using append() method, adding a string world to our StringBuilder builder = builder.append(" world!"); // creating a new String variable that has a StringBuilder value String result = builder.toString(); System.out.println("Our string after using an append() method: " + result); } }
Note
Be extremely cautious when using
StringBuilder
, and remember that aString
cannot be modified after initialization. If you encounter a situation in your program where you think your methods are not working, double-check whether you have considered this.
Swipe to begin your solution
Create a string that lists products with their prices for display on a webpage. Use StringBuilder
to construct the string.
- Add the header
"Product List:\n"
to theStringBuilder
(where\n
represents a line break). - Loop through the array using a
for-each
loop. - Get the product name from each array element.
- Get the product price from each array element.
- Append the product name and price to the
StringBuilder
in the correct format (Laptop - $999.99). - Output the content of the
StringBuilder
to the console.
Solución
solution
¡Gracias por tus comentarios!