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

Зміст курсу

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

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.

Let's take a look at an example:

java

Main

1234567891011
package com.example; public class Main { // do not change code from below public static void main(String[] args) { String string = " This string contains unnecessary space symbols "; System.out.println("String before using trim() method: " + string); System.out.println("String after using trim() method: " + string.trim()); } // do not change the code above }

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?

What will be the output of the code from below?

Виберіть правильну відповідь

What will be the output of the code from below?

Виберіть правильну відповідь

What will be the output of the code from below?

Виберіть правильну відповідь

What will be the output of the code from below?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 3. Розділ 6
We're sorry to hear that something went wrong. What happened?
some-alt