Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript equivalent of join() and toString() in c#?

Tags:

string

c#

is there any method in c# thats equivalent to the javascript join()..

   var keyStr = keyList.join("_");

My requirement is to concatenate the array of strings into an single string with the given separator.

And i wanted to convert my whole string array into an single string... in javascript we can do this by calling toString() of the jabvascript array

C# toString of an array just prints the type information. If we use toString on other types like int, it returns the string representation of an int. But why this is been not implemented in String array. wouldnt that strange??

And

like image 426
RameshVel Avatar asked Sep 18 '09 07:09

RameshVel


2 Answers

You can use string.Join():

string.Join("_", array);

or, for lists:

string.Join("_", list);

Converting a string array into a single string is done exactly the same way: With string.Join():

string.Join(" ", stringarray);

Dan Elliott also has a nice extension method you can use to be a little closer to JavaScript, syntax-wise.

like image 127
Joey Avatar answered Oct 23 '22 14:10

Joey


if you wish to add the functionality to a string array you could do with an extension method

public static class ArrayExtension
{

  public static string AsString(this string[] array, string seperator)
  {
    return string.Join(seperator, array);
  }
}

Then you would write:

var keyStr = keyList.AsString("_");
like image 29
Daniel Elliott Avatar answered Oct 23 '22 15:10

Daniel Elliott