Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi everyone...

i want to know how to con-cat or combine strings by using for loop in java...
Posted
Comments
Sergey Alexandrovich Kryukov 4-Apr-15 0:23am    
What's wrong with just reading Java documentation/reference? This is a forum for software developer...
—SA

No need for loop when you just want to concatenate 2 strings:
String firstString = "Hello";
String secondString  = "World";
String sayHello  = firstString + " " + secondString;

Find out more: http://www.tutorialspoint.com/java/java_strings.htm[^]
 
Share this answer
 
v2
The answer is: using '+' operator. Alternatively, you can use the String method String concat(String): http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#concat(java.lang.String).

The "using for loop" part simply makes no sense. Do with your loop whatever you need to, don't mix different things together. When you ask a "how to" question, you should not mix it with you understanding of "how" to do it.

—SA
 
Share this answer
 
Here your solution,,,

Java
public static void main(String[] args) {
        String str1 = "Ajith ";
        String str2 = "Kp";
        int n1 = str1.length();
        int n2 = str2.length();
        char[] chr = new char[n1+n2];
        int ind = 0;
        for(int i=0;i<n1;i++)
        {
            chr[ind] = 0;
            chr[ind++] = str1.charAt(i);
        }
        for(int i=0;i<n2;i++)
        {
            chr[ind] = 0;
            chr[ind++] = str2.charAt(i);
        }
        String str = String.valueOf(chr);
        System.out.println(str);
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900