Convert List of object to string array C

String.Join Method

  • Reference
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.

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 article

Overloads

Join[Char, Object[]]

Concatenates the string representations of an array of objects, using the specified separator between each member.

Join[Char, String[]]

Concatenates an array of strings, using the specified separator between each member.

Join[String, IEnumerable]

Concatenates the members of a constructed IEnumerable collection of type String, using the specified separator between each member.

Join[String, Object[]]

Concatenates the elements of an object array, using the specified separator between each element.

Join[String, String[]]

Concatenates all the elements of a string array, using the specified separator between each element.

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.

Join[String, String[], Int32, Int32]

Concatenates the specified elements of a string array, using the specified separator between each element.

Join[Char, IEnumerable]

Concatenates the members of a collection, using the specified separator between each member.

Join[String, IEnumerable]

Concatenates the members of a collection, using the specified separator between each member.

Join[Char, Object[]]

Concatenates the string representations of an array of objects, using the specified separator between each member.

public: static System::String ^ Join[char separator, ... cli::array ^ values]; public static string Join [char separator, params object?[] values]; public static string Join [char separator, params object[] values]; static member Join : char * obj[] -> string Public Shared Function Join [separator As Char, ParamArray values As Object[]] As String

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.

Returns

String

A 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 to

Join[Char, String[]]

Concatenates an array of strings, using the specified separator between each member.

public: static System::String ^ Join[char separator, ... cli::array ^ value]; public static string Join [char separator, params string?[] value]; public static string Join [char separator, params string[] value]; static member Join : char * string[] -> string Public Shared Function Join [separator As Char, ParamArray value As String[]] As String

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.

Returns

String

A 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]

Concatenates the members of a constructed IEnumerable collection of type String, using the specified separator between each member.

public: static System::String ^ Join[System::String ^ separator, System::Collections::Generic::IEnumerable ^ values]; public static string Join [string separator, System.Collections.Generic.IEnumerable values]; public static string Join [string? separator, System.Collections.Generic.IEnumerable values]; [System.Runtime.InteropServices.ComVisible[false]] public static string Join [string separator, System.Collections.Generic.IEnumerable values]; static member Join : string * seq -> string [] static member Join : string * seq -> string Public Shared Function Join [separator As String, values As IEnumerable[Of String]] As String

Parameters

separator String

The string to use as a separator.separator is included in the returned string only if values has more than one element.

values IEnumerable

A collection that contains the strings to concatenate.

Returns

String

A string that consists of the elements of values delimited by the separator string.

-or-

Empty if values has zero elements.

Attributes
ComVisibleAttribute

Exceptions

ArgumentNullException

values is null.

OutOfMemoryException

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 object of type String, which it then passes to the Join[String, IEnumerable] method.

using System; using System.Collections.Generic; public class Example { public static void Main[] { int maxPrime = 100; List primes = GetPrimes[maxPrime]; Console.WriteLine["Primes less than {0}:", maxPrime]; Console.WriteLine[" {0}", String.Join[" ", primes]]; } private static List 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 string Public Shared Function Join [separator As String, ParamArray values As Object[]] As String

Parameters

separator String

The string to use as a separator. separator is included in the returned string only if values has more than one element.

values Object[]

An array that contains the elements to concatenate.

Returns

String

A 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.

Attributes
ComVisibleAttribute

Exceptions

ArgumentNullException

values is null.

OutOfMemoryException

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 -> string Public Shared Function Join[Of T] [separator As Char, values As IEnumerable[Of T]] As String

Type Parameters

T

The type of the members of values.

Parameters

separator Char

The character to use as a separator. separator is included in the returned string only if values has more than one element.

values IEnumerable

A collection that contains the objects to concatenate.

Returns

String

A string that consists of the members of values delimited by the separator character.

-or-

Empty if values has no elements.

Exceptions

ArgumentNullException

values is null.

OutOfMemoryException

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.

public: generic static System::String ^ Join[System::String ^ separator, System::Collections::Generic::IEnumerable ^ values]; public static string Join [string separator, System.Collections.Generic.IEnumerable values]; public static string Join [string? separator, System.Collections.Generic.IEnumerable values]; [System.Runtime.InteropServices.ComVisible[false]] public static string Join [string separator, System.Collections.Generic.IEnumerable values]; static member Join : string * seq -> string Public Shared Function Join[Of T] [separator As String, values As IEnumerable[Of T]] As String

Type Parameters

T

The type of the members of values.

Parameters

separator String

The string to use as a separator. separator is included in the returned string only if values has more than one element.

values IEnumerable

A collection that contains the objects to concatenate.

Returns

String

A string that consists of the elements of values delimited by the separator string.

-or-

Empty if values has no elements.

Attributes
ComVisibleAttribute

Exceptions

ArgumentNullException

values is null.

OutOfMemoryException

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 object of type integer, which it then passes to the Join[String, IEnumerable] method.

using System; using System.Collections.Generic; public class Example { public static void Main[] { int maxPrime = 100; List primes = GetPrimes[maxPrime]; Console.WriteLine["Primes less than {0}:", maxPrime]; Console.WriteLine[" {0}", String.Join[" ", primes]]; } private static List 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

Chủ Đề