How to remove leading and trailing space from string - Java Language Basics

Java examples for Language Basics:String

Description

How to remove leading and trailing space from string

Demo Code


public class Main {
 
        public static void main(String[] args) {
               //from w ww .java 2 s .  c  om
                String str = "   String Trim Example   ";
               
                String strTrimmed = str.trim();
                 
                System.out.println("Original String is: " + str);
                System.out.println("Removed Leading and trailing space");
                System.out.println("New String is: " + strTrimmed);
        }
}
 

Result


Related Tutorials