Convert List of object to string array C
String.Join Method
Is this page helpful?
Please rate your experience
Yes
No
Any additional feedback?
Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy. Show
Submit
Thank you. Definition
Namespace:
System
Assembly:System.Runtime.dll
Assembly:netstandard.dll
Assembly:mscorlib.dll
Important Some information relates to prerelease product that may be substantially modified before its released. Microsoft makes no warranties, express or implied, with respect to the information provided here. Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. In this articleOverloads
Join(Char, Object[])Concatenates the string representations of an array of objects, using the specified separator between each member. Parameters
separator
Char
The character to use as a separator. separator is included in the returned string only if value has more than one element.
values
Object[]
An array of objects whose string representations will be concatenated. ReturnsStringA string that consists of the elements of values delimited by the separator character. -or- Empty if values has zero elements. Exceptions
ArgumentNullException
value is null.
OutOfMemoryException
The length of the resulting string overflows the maximum allowed length (MaxValue). Applies toJoin(Char, String[])Concatenates an array of strings, using the specified separator between each member. Parameters
separator
Char
The character to use as a separator. separator is included in the returned string only if value has more than one element.
value
String[]
An array of strings to concatenate. ReturnsStringA string that consists of the elements of value delimited by the separator character. -or- Empty if value has zero elements. Exceptions
ArgumentNullException
value is null.
OutOfMemoryException
The length of the resulting string overflows the maximum allowed length (MaxValue). Applies to
Join(String, IEnumerable
|
Join(String, Object[])
Concatenates the elements of an object array, using the specified separator between each element.
Parameters
The string to use as a separator. separator is included in the returned string only if values has more than one element.
An array that contains the elements to concatenate.
Returns
StringA string that consists of the elements of values delimited by the separator string.
-or-
Empty if values has zero elements.
-or-
.NET Framework only: Empty if the first element of values is null.
AttributesExceptions
values is null.
The length of the resulting string overflows the maximum allowed length (MaxValue).
Examples
The following example uses the Sieve of Eratosthenes algorithm to calculate the prime numbers that are less than or equal to 100. It assigns the result to a integer array, which it then passes to the Join(String, Object[]) method.
using System; using System.Collections.Generic; public class Example { public static void Main() { int maxPrime = 100; int[] primes = GetPrimes(maxPrime); Console.WriteLine("Primes less than {0}:", maxPrime); Console.WriteLine(" {0}", String.Join(" ", primes)); } private static int[] GetPrimes(int maxPrime) { Array values = Array.CreateInstance(typeof(int), new int[] { maxPrime - 1}, new int[] { 2 }); // Use Sieve of Eratosthenes to determine prime numbers. for (int ctr = values.GetLowerBound(0); ctr <= (int) Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++) { if ((int) values.GetValue(ctr) == 1) continue; for (int multiplier = ctr; multiplier <= maxPrime / 2; multiplier++) if (ctr * multiplier <= maxPrime) values.SetValue(1, ctr * multiplier); } ListRemarks
If separator is null or if any element of values other than the first element is null, an empty string (String.Empty) is used instead. See the Notes for Callers section if the first element of values is null.
Join(String, Object[]) is a convenience method that lets you concatenate each element in an object array without explicitly converting its elements to strings. The string representation of each object in the array is derived by calling that object's ToString method.
Notes to Callers
.NET Framework only: If the first element of values is null, the Join(String, Object[]) method does not concatenate the elements in values but instead returns Empty. A number of workarounds for this issue are available. The easiest is to assign a value of Empty to the first element of the array, as the following example shows.
object[] values = { null, "Cobb", 4189, 11434, .366 }; if (values[0] == null) values[0] = String.Empty; Console.WriteLine(String.Join("|", values)); // The example displays the following output: // |Cobb|4189|11434|0.366 Dim values() As Object = { Nothing, "Cobb", 4189, 11434, .366 } If values(0) Is Nothing Then values(0) = String.Empty Console.WriteLine(String.Join("|", values)) ' The example displays the following output: ' |Cobb|4189|11434|0.366See also
- Split(Char[])
Applies to
Join(String, String[])
Concatenates all the elements of a string array, using the specified separator between each element.
Parameters
The string to use as a separator. separator is included in the returned string only if value has more than one element.
An array that contains the elements to concatenate.
Returns
StringA string that consists of the elements in value delimited by the separator string.
-or-
Empty if values has zero elements.
Exceptions
value is null.
The length of the resulting string overflows the maximum allowed length (MaxValue).
Examples
The following example demonstrates the Join method.
using namespace System; String^ MakeLine( int initVal, int multVal, String^ sep ) { arrayRemarks
For example, if separator is ", " and the elements of value are "apple", "orange", "grape", and "pear", Join(separator, value) returns "apple, orange, grape, pear".
If separator is null, an empty string (String.Empty) is used instead. If any element in value is null, an empty string is used instead.
See also
- Concat(Object)
Applies to
Join(Char, String[], Int32, Int32)
Concatenates an array of strings, using the specified separator between each member, starting with the element in value located at the startIndex position, and concatenating up to count elements.
Parameters
Concatenates an array of strings, using the specified separator between each member, starting with the element located at the specified index and including a specified number of elements.
An array of strings to concatenate.
The first item in value to concatenate.
The number of elements from value to concatenate, starting with the element in the startIndex position.
Returns
StringA string that consists of count elements of value starting at startIndex delimited by the separator character.
-or-
Empty if count is zero.
Exceptions
value is null.
startIndex or count are negative.
-or-
startIndex is greater than the length of value - count.
The length of the resulting string overflows the maximum allowed length (MaxValue).
Applies to
Join(String, String[], Int32, Int32)
Concatenates the specified elements of a string array, using the specified separator between each element.
Parameters
The string to use as a separator. separator is included in the returned string only if value has more than one element.
An array that contains the elements to concatenate.
The first element in value to use.
The number of elements of value to use.
Returns
StringA string that consists of count elements of value starting at startIndex delimited by the separator character.
-or-
Empty if count is zero.
Exceptions
value is null.
startIndex or count is less than 0.
-or-
startIndex plus count is greater than the number of elements in value.
Out of memory.
Examples
The following example concatenates two elements from an array of names of fruit.
// Sample for String::Join(String, String[], int int) using namespace System; int main() { arrayRemarks
For example, if separator is ", " and the elements of value are "apple", "orange", "grape", and "pear", Join(separator, value, 1, 2) returns "orange, grape".
If separator is null, an empty string (String.Empty) is used instead. If any element in value is null, an empty string is used instead.
See also
- Split(Char[])
Applies to
Join(Char, IEnumerable)
Concatenates the members of a collection, using the specified separator between each member.
Type Parameters
The type of the members of values.
Parameters
The character to use as a separator. separator is included in the returned string only if values has more than one element.
A collection that contains the objects to concatenate.
Returns
StringA string that consists of the members of values delimited by the separator character.
-or-
Empty if values has no elements.
Exceptions
values is null.
The length of the resulting string overflows the maximum allowed length (MaxValue).
Applies to
Join(String, IEnumerable)
Concatenates the members of a collection, using the specified separator between each member.
Type Parameters
The type of the members of values.
Parameters
The string to use as a separator. separator is included in the returned string only if values has more than one element.
A collection that contains the objects to concatenate.
Returns
StringA string that consists of the elements of values delimited by the separator string.
-or-
Empty if values has no elements.
AttributesExceptions
values is null.
The length of the resulting string overflows the maximum allowed length (MaxValue).
Examples
The following example uses the Sieve of Eratosthenes algorithm to calculate the prime numbers that are less than or equal to 100. It assigns the result to a List
Remarks
If separator is null, an empty string (String.Empty) is used instead. If any member of values is null, an empty string is used instead.
Join
This method is particular useful with Language-Integrated Query (LINQ) query expressions. For example, the following code defines a very simple Animal class that contains the name of an animal and the order to which it belongs. It then defines a List
See also
- Split(Char[])