Saturday, May 12, 2018

Java String concat() Method with Example

This method is use to concatenate the string at the end of other string. This method return a String with appened String at the end of the original String.

Syntax:

public String concat(String s)

s- the string that is concatenate to the end of this string.


Example 1:

public class Test {

   public static void main(String args[]) {
      String s = "Java is a simple language";
      s = s.concat(" and also it is robust in nature");
      System.out.println(s);
  
   }
}

Output:

Java is a simple language and also it is robust in nature




Note:- This method is called multiple times in a single statement.

Example 2:

public class StringConcatenate {

public static void main(String args[]) {
//One way of doing concatenation
       String str1 = "Welcome";
       str1 = str1.concat(" to ");
       str1 = str1.concat(" String concatenation ");
       System.out.println(str1);

       //Other way of doing concatenation in one line
       String str2 = "This";
       str2 = str2.concat(" is").concat(" for your").concat(" helping");
       System.out.println(str2);
   }

}

Output:

Welcome to  String concatenation
This is for your helping





                                                                                              trim() method in String


No comments:

Post a Comment