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
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.
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("_");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With