Magic
Magic

Reputation: 137

Appending Strings in Java/C# without using StringBuffer.Append or StringBuilder.Append

at a recent interview I attended, the programming question that was asked was this. Write a function that will take as input two strings. The output should be the result of concatenation.

Conditions: Should not use StringBuffer.Append or StringBuilder.Append or string objects for concatenation;that is, they want me to implement the pseudo code implementation of How StringBuilder or StringBuffer's Append function works.

This is what I did:

    static char[] AppendStrings(string input, string append)
    {
        char[] inputCharArray = input.ToCharArray();
        char[] appendCharArray = append.ToCharArray();
        char[] outputCharArray = new char[inputCharArray.Length + appendCharArray.Length];
        for (int i = 0; i < inputCharArray.Length; i++)
        {
            outputCharArray[i] = inputCharArray[i];
        }
        for (int i = 0; i < appendCharArray.Length; i++)
        {
            outputCharArray[input.Length + i] = appendCharArray[i];
        }
        return outputCharArray;
    }

While this is a working solution, is there a better way of doing things?

Upvotes: 4

Views: 3158

Answers (5)

chintan kalathiya
chintan kalathiya

Reputation: 79

java default support "+" for append string

 String temp="some text";
   for(int i=0;i<10;i++)
{
  temp=temp+i;
}

Or

   temp=temp+" some other text"

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533930

In Java you can just use concat which does not use StringBuilder or StringBuffer.

String a = "foo";
String b = "bar";
String ab = a.concat(b);

The source for String.concat(String) from Oracle's JDK.

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

Upvotes: 0

SLaks
SLaks

Reputation: 888303

You can call CopyTo:

char[] output = new char[a.Length + b.Length];
a.CopyTo(0, output, 0, a.Length);
b.CopyTo(0, output, a.Length, b.Length);

return new String(output);

If they don't like that, call .ToCharArray().CopyTo(...).

You can also cheat:

return String.Join("", new [] { a, b });

return String.Format("{0}{1}", a, b);

var writer = new StringWriter();
writer.Write(a);
writer.Write(b);
return writer.ToString();

Upvotes: 5

BrokenGlass
BrokenGlass

Reputation: 161012

is LINQ legal? strings are just can be treated as an enumeration of chars, so they can be used with LINQ (even though there is some cost involved, see comments):

string a = "foo";
string b = "bar";

string c = new string(a.AsEnumerable().Concat(b).ToArray());

or with your method signature:

 static char[] AppendStrings(string input, string append)
 {
   return input.AsEnumerable().Concat(append).ToArray();
 }

Upvotes: 5

JaredPar
JaredPar

Reputation: 755587

I would've done something like the following (argument checking omitted for brevity)

public static string Append(string left, string right) {
  var array = new char[left.Length + right.Length];
  for (var i = 0; i < left.Length; i++) {
    array[i] = left[i];
  }
  for (var i = 0; i < right.Length; i++) {
    array[i + left.Length] = right[i];
  }
  return new string(array);
}

Upvotes: 2

Related Questions