Java concat() method

    concat() method

    Java concat() method will allow you to combine the provided string at the end of the current string and the combined string will be returned. It almost works like appending the two string.

    Method implementation-

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

    Syntax-

    public String concat(String str)

    Example-

    public class Simple{  
    
        public static void main(String args[]){  
            String s1="combining string";  
            s1.concat("is easy");  
            System.out.println(s1);  
            s1=s1.concat(" to implement");  
            System.out.println(s1);  
         }
    }  

    Output-

    Java String Combine Method

    Concatenating multiple strings example-

    public class Simple{  
    
    public static void main(String[] args) {      
    
            String str1 = "Hello";  
    
            String str2 = "Welcome";  
    
            String str3 = "Students";  
    
          
    
            String str4 = str1.concat(str2);          
    
            System.out.println(str4);  
    
            String str5 = str1.concat(str2).concat(str3);  
    
            System.out.println(str5);  
    
        }  
    
    }  

    Output-

    Concatenating with special characters example-

    public class Simple{  
    
    public static void main(String[] args) {      
    
            String str1 = "Hello";  
    
            String str2 = "Students";  
    
            String str3 = "Read Java";  
    
         
    
            String str4 = str1.concat(" ").concat(str2).concat(" ").concat(str3);  
    
            System.out.println(str4);         
    
            
    
            String str5 = str1.concat("!!!");  
    
            System.out.println(str5);         
    
        }  
    
    } 

    Output-