A blog for everybody who loves coding.

concatenate two strings in java using loop

in this post we will see how to write a program to concatenate two strings in java using loop. there are many ways to concatenate string.

concatenate two string in java using Loop

class StringConCatination1{
    public static void main(String[] args) {
        String str1 = "String1 ";
        String str2 = "String2";
        int size1 = str1.length();
        int size2 = str2.length();
        char chr[] = new char[size1+size2];
        int n = 0;
        for(int i=0;i<size1;i++)
        {
            chr[n++] = str1.charAt(i);
        }
        for(int i=0;i<size2;i++)
        {
            chr[n++] = str2.charAt(i);
        }
        String str3 = String.valueOf(chr);
        System.out.println(str3);
    }
}

output

C:\Users\Desktop\JAVA PROGRAMS>javac StringConCatination1.java

C:\Users\Desktop\JAVA PROGRAMS>java StringConCatination1
String1 String2

string concatenation using concat() function

class Concat { 
    public static void main(String args[]) 
    { 
        String s = "string"; 
        s = s.concat("is string); 
        System.out.println(s); 
    } 
} 

output

string is string
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on whatsapp
WhatsApp