Languages
[Edit]
EN

Java - trim whitespace from string

0 points
Created by:
Brian-Tompset
491

In this article, we would like to show you how to trim whitespace from string in Java.

Quick solution:

String text = "   abc    ";

String result = text.trim();

System.out.println(result);

 

Practical example

In this example, we use String trim() method to remove whitespaces from the beginning and from the end of the strings.

public class Example {

    public static void main(String[] args) throws Exception {
        String text1 = "   abc";
        String text2 = "abc   ";
        String text3 = "   abc   ";

        String result1 = text1.trim();
        String result2 = text2.trim();
        String result3 = text3.trim();

        System.out.println(result1);  // abc
        System.out.println(result2);  // abc
        System.out.println(result3);  // abc
    }
}

Output:

abc
abc
abc

References

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - trim whitespace from string

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join