Home >>Java String Methods >Java String concat() method

Java String concat() method

Java String concat

The concat() method for java string combines defined string at the end of this string. Combined string returns. It's like having another string appended.

Internal implementation


public String concat(String str) 
{  
       int len1 = str.length();  
       if (len1 == 0) {  
           return this;  
       }  
       int len = value.length;  
       char buf[] = Arrays.copyOf(value, len + len1);  
       str.getChars(buf, len);  
       return new String(buf, true);  
   }  

Signature

public String concat(String anotherString) 

Parameter

anotherString : a separate string to be combined at the end of this string.

Returns

combined string

String concat() method example1

public class ConcatExample
{
public static void main(String args[])
{
String n1="phptpoint";
n1.concat("is immutable");
System.out.println(n1);
n1=n1.concat(" provides full course to learn");
System.out.println(n1);
}}

Output
phptpoint
phptpoint Provides full course to learn
String concat() Method Example 2

public class ConcatExample2 
{  
    public static void main(String[] args) 
	{      
        String con1 = "Hello";  
        String con2 = "Phptpoint";  
        String con3 = "User";  
        String con4 = con1.concat(con2);          
        System.out.println(con4);  
        String con5 = con1.concat(con2).concat(con3);  
        System.out.println(con5);  
    }  
}  

Output
HelloPhptpoint
HelloPhptpointUser
String concat() Method Example 3

public class ConcatExample3 
{  
    public static void main(String[] args) 
	{  
        String num1 = "Hello";  
        String num2 = "Phptpoint";  
        String num3 = "User";  
        String num4 = num1.concat(" ").concat(num2).concat(" ").concat(num3);  
        System.out.println(num4);         
        String num5 = num1.concat("…");  
        System.out.println(num5);         
        String num6 = num1.concat("*").concat(num2);  
        System.out.println(num6);  
    }  
}  


Output
Hello Phptpoint User
Hello…
Hello*Phptpoint

No Sidebar ads