Adding strings in Java / C # without using StringBuffer.Append or StringBuilder.Append

In a recent interview in which I participated, I asked the programming question that was asked. Write a function that will take two lines as input. The result should be the result of concatenation.

Conditions: you should not use StringBuffer.Append or StringBuilder.Append or string objects for concatenation, that is, they want me to implement the implementation of pseudocode in the StringBuilder or StringBuffer Append function.

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 to do something?

+4
source share
5 answers

is LINQ legal? strings - it's simple , which can be considered as an enumeration of characters, so they can be used with LINQ (although there are some costs, 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(); } 
+4
source

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 it, call .ToCharArray().CopyTo(...) .

You can also fool:

 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(); 
+4
source

I would do 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); } 
+2
source

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

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

Source for String.concat (String) from Oracle 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); } 
0
source

java "+" default support for adding a line

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

Or

  temp=temp+" some other text" 
0
source

Source: https://habr.com/ru/post/1346812/


All Articles