user3296744
user3296744

Reputation: 169

How to concat 2 string without + concat() and StringBuffer in java

import java.util.ArrayList;

public class ConcatString {

public static void main(String[] args) {
        // TODO Auto-generated method stub

    ArrayList list=new ArrayList();

    String[] str={"Hello","World!!!!","Java"};
    for(int i=0;i<str.length;i++)
    {
        list.add(str[i]);
    }
    for(int i=0;i<str.length;i++)
    {
        System.out.print(list.get(i));
    }
}

Is this the right approach since i am new to java? Concat using no inbuiltin functions or + or StringBuffer...Its an interview question

Upvotes: 0

Views: 370

Answers (6)

klarki
klarki

Reputation: 915

I assume you can't use StringBuffer and you can't use StringBuilder either, as this wouldn't make sense otherwise.

You can concat two Strings after converting them to a char array:

public String concat(String[] args){
    int length = 0;
    for (String s : args) {
        length += s.length();
    }
    char[] result = new char[length];

    int i = 0;

    for (String s : args) {
        s.getChars(0, s.length(), result, i);
        i += s.length();
    }

    return new String(result);
}

Here's how you test it:

public void testConcat() {
    String[] s = new String[3];
    s[0] = "abc";
    s[1] = "def";
    s[2] = "ghi";
    System.out.print(concat(s));

    /*
     * Output: abcdefghi
     */
}

Upvotes: 0

JossVAMOS
JossVAMOS

Reputation: 310

Like this :

    String[] str={"Hello","World!!!!","Java"};
    String concat ="";
    for(String s : str)
    {
        concat+=s;
    }
    System.out.println(concat);

Upvotes: 1

user3467480
user3467480

Reputation: 93

You dont need Arraylist for concatenation. Try the below approach

    String[] str={"Hello","World!!!!","Java"};
    StringBuilder finalString = new StringBuilder();
    for(int i=0;i<str.length;i++)
    {
        finalString.append(str[i]);
    }
    System.out.println(finalString);

Upvotes: -1

Salah
Salah

Reputation: 8657

You could use one simple one of the following

String[] str = {"Hello","World!!!!","Java"};
String concat ="";
for(String s : str)
{
    concat+=s;
}
System.out.println(concat);

Or using StringBuilder which is more efficient like this:

String[] str={"Hello","World!!!!","Java"};
StringBuilder sb = new StringBuilder();
for(String s : str)
{
    sb.append(s);
}
System.out.println(sb.toString());

Upvotes: -1

merlin2011
merlin2011

Reputation: 75555

If your string array is large, you want to use StringBuilder, because using the += string concatenation is inefficient due to Java String immutability.

String[] str={"Hello","World!!!!","Java"};
StringBuilder sb = new StringBuilder();
for(String s : str)
{
    sb.append(s);
}
System.out.println(sb.toString());

Upvotes: 3

Don Chakkappan
Don Chakkappan

Reputation: 7560

What is your target?.Here you simply copies the values from string array and stored it in an ArrayList.Where the code for concatination.To string concatination in java just use + operator.Don't go for complicated logic , if you have simple alternatives.

Upvotes: 0

Related Questions