Thursday, December 24, 2015

concat() and trim() method

concat()

You can concat two String using concat() as shown below:


package stringdemo;


public class Concat {
    public static void main(String[] args) {
        String s1="ram";
        String s2="john";
        
        System.out.println(s1.concat(s2));
    }

}
The output of the above program is:
ramjohn



trim()

trim() method of String in java is used to remove whitespace.It has the general form:
String trim();
Here is a small program to demonstrate trim()method:

package stringdemo;


public class Trim {
    public static void main(String[] args) {
        String s="  Hello World";
        
        System.out.println(s.trim());
    }
}
The output of the above program is:
Hello World

0 comments:

Post a Comment