Trimming Whitespace - Java Language Basics

Java examples for Language Basics:String

Introduction

Use the String trim() method to eliminate the whitespace.

Demo Code

public class Main {
    public static void main(String[] args){
        String myString = " This is a String that contains whitespace.   ";
        //from w ww .  j  av a 2s. c  om
        System.out.println(myString);
        System.out.println(myString.trim());
    }
   
}

Result


Related Tutorials