How to remove the leading and trailing whitespaces in a given String using the Java trim() method

Опубликовано: 01 Октябрь 2024
на канале: Career & Tech HQ
1,666
15

How to remove the leading and trailing whitespaces in a given String using the Java trim() method

In this video, we will have a look at the Java trim() method, which is a method used to get rid of the leading and trailing white spaces in a given String. The trim() method returns a String value after removing the leading and trailing white spaces.

Let’s take an example, to show you how this method works

String str = “ I love programming “;
System.out.println(“Our String value after applying the trim method : “ + str.trim());

After running this program, the result will be : I love programming
You can notice that, the method has removed all the leading and trailing whitespaces, but the whitespaces between the words were retained.

#codingriver
#java
#programming