I would like to convert a string array to a single string.
string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
I would like to have something like "Hello World!"
string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
string.Join("", test);
var separator = ", "; string.Join(separator , test);string.Join(string.Empty, test) rather than using an empty-paranthesis.A slightly faster option than using the already mentioned use of the Join() method is the Concat() method. It doesn't require an empty delimiter parameter as Join() does. Example:
string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
string result = String.Concat(test);
hence it is likely faster.
Concat is better, in inner code of Join, it will append separator after each item. But Concat not have such codes, it is simpler and more direct than Join. This is the fittst answer.A simple string.Concat() is what you need.
string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
string result = string.Concat(test);
If you also need to add a seperator (space, comma etc) then, string.Join() should be used.
string[] test = new string[2];
test[0] = "Red";
test[1] = "Blue";
string result = string.Join(",", test);
If you have to perform this on a string array with hundereds of elements than string.Join() is better by performace point of view. Just give a "" (blank) argument as seperator. StringBuilder can also be used for sake of performance, but it will make code a bit longer.
Concat and Join are faster than the StringBuilder if you have 4 or less strings to concatenate.Try:
String.Join("", test);
which should return a string joining the two elements together. "" indicates that you want the strings joined together without any separators.
"Hello " in the question already had a trailing space. If the space was not already there, then you would be correct, and using " " as a separator would make more sense.In the accepted answer, String.Join isn't best practice per its usage. String.Concat should have be used since OP included a trailing space in the first item: "Hello " (instead of using a null delimiter).
However, since OP asked for the result "Hello World!", String.Join is still the appropriate method, but the trailing whitespace should be moved to the delimiter instead.
// string[] test = new string[2];
// test[0] = "Hello ";
// test[1] = "World!";
string[] test = { "Hello", "World" }; // Alternative array creation syntax
string result = String.Join(" ", test);
Aggregate can also be used for same.
string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
string joinedString = test.Aggregate((prev, current) => prev + " " + current);
String.Join due to multiple extra strings constructed. Please read stackoverflow.com/questions/217805/… for proper variant of Aggregate. string ConvertStringArrayToString(string[] array)
{
//
// Concatenate all the elements into a StringBuilder.
//
StringBuilder strinbuilder = new StringBuilder();
foreach (string value in array)
{
strinbuilder.Append(value);
strinbuilder.Append(' ');
}
return strinbuilder.ToString();
}
string.Join(" ", array) (except yours adds a trailing space). Why not just use the one in the library?