Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende trim() Method | String Advanced
Java Extended

book
trim() Method

How can we get rid of extra space characters?

Sometimes a string may have leading or trailing spaces, which can cause issues in our code. However, Java developers have provided a solution for this by introducing the trim() method. What does it do? This method removes leading and trailing spaces from a string. If there are no leading or trailing spaces, the trim() method simply returns the original string. If the entire string consists of spaces, the trim() method will return an empty string.

Main.java

Main.java

copy
package com.example;

// do not modify the code below this comment

public class Main {
public static void main(String[] args) {
String string = " This string contains unnecessary space symbols ";
// print the string before using the trim() method
System.out.println("String before using trim() method: " + string);
// print the string after using the trim() method, which removes leading and trailing spaces
System.out.println("String after using trim() method: " + string.trim());
}
}
12345678910111213
package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { String string = " This string contains unnecessary space symbols "; // print the string before using the trim() method System.out.println("String before using trim() method: " + string); // print the string after using the trim() method, which removes leading and trailing spaces System.out.println("String after using trim() method: " + string.trim()); } }

That's essentially all we can say about this method. However, the trim() method does a lot of good and significantly simplifies working with strings.

1. What will be the output of the code from below?

2. What will be the output of the code from below?

3. What will be the output of the code from below?

4. What will be the output of the code from below?

question mark

What will be the output of the code from below?

String str = "Hello World";
int index = str.indexOf("o");
System.out.println(index);

Select the correct answer

question mark

What will be the output of the code from below?

String str = "Hello World";
int lastIndex = str.lastIndexOf("o");
System.out.println(lastIndex);

Select the correct answer

question mark

What will be the output of the code from below?

String str = "Java,Java,Java";
String[] parts = str.split(",");
System.out.println(parts.length);

Select the correct answer

question mark

What will be the output of the code from below?

String str = " Hello World ";
String trimmedStr = str.trim();
System.out.println(trimmedStr);

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 6

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

We use cookies to make your experience better!
some-alt