Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende StringBuilder | String
Principios Básicos de Java
course content

Contenido del Curso

Principios Básicos de Java

Principios Básicos de Java

1. Iniciando
2. Tipos Básicos, Operaciones
3. Loops
4. Arrays
5. String

book
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:

java

Main

copy
1
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:

java

Main

copy
123456789
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 a NullPointerException (NPE) being thrown.

java

Main

copy
123456789
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.

java

Main

copy
12345678910111213141516
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 a String 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.

Tarea
test

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.

  1. Add the header "Product List:\n" to the StringBuilder (where \n represents a line break).
  2. Loop through the array using a for-each loop.
  3. Get the product name from each array element.
  4. Get the product price from each array element.
  5. Append the product name and price to the StringBuilder in the correct format (Laptop - $999.99).
  6. Output the content of the StringBuilder to the console.

Solución

java

solution

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 6
toggle bottom row

book
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:

java

Main

copy
1
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:

java

Main

copy
123456789
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 a NullPointerException (NPE) being thrown.

java

Main

copy
123456789
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.

java

Main

copy
12345678910111213141516
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 a String 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.

Tarea
test

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.

  1. Add the header "Product List:\n" to the StringBuilder (where \n represents a line break).
  2. Loop through the array using a for-each loop.
  3. Get the product name from each array element.
  4. Get the product price from each array element.
  5. Append the product name and price to the StringBuilder in the correct format (Laptop - $999.99).
  6. Output the content of the StringBuilder to the console.

Solución

java

solution

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 6
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt