String.Concat Method (IEnumerable(Of String))
Concatenates the members of a constructed IEnumerable(Of T) collection of type String.
Assembly: mscorlib (in mscorlib.dll)
<ComVisibleAttribute(False)> Public Shared Function Concat ( values As IEnumerable(Of String) ) As String
Parameters
- values
-
Type:
System.Collections.Generic.IEnumerable(Of String)
A collection object that implements IEnumerable(Of T) and whose generic type argument is String.
Return Value
Type: System.StringThe concatenated strings in values, or String.Empty if values is an empty IEnumerable(Of String).
| Exception | Condition |
|---|---|
| ArgumentNullException | values is null. |
The method concatenates each object in values; it does not add any delimiters. To specify a delimiter between each member of values, call the Join(String, IEnumerable(Of String)) method.
An Empty string is used in place of any null element in values.
If values is an empty IEnumerable(Of String), the method returns String.Empty. If values is null, the method throws an ArgumentNullException exception.
Concat(IEnumerable(Of String)) is a convenience method that lets you concatenate each element in an IEnumerable(Of String) collection without first converting the elements to a string array. It is particularly useful with Language-Integrated Query (LINQ) query expressions. The following example passes a List(Of String) object that contains either the uppercase or lowercase letters of the alphabet to a lambda expression that selects letters that are equal to or greater than a particular letter (which, in the example, is "M"). The IEnumerable(Of String) collection that is returned by the Enumerable.Where(Of TSource) method is passed to the Concat(IEnumerable(Of String)) method to display the result as a single string.
Imports System.Collections.Generic Imports System.Linq Module modMain Public Sub Main() Dim output As String = String.Concat(GetAlphabet(true).Where(Function(letter) _ letter >= "M")) Console.WriteLine(output) End Sub Private Function GetAlphabet(upper As Boolean) As List(Of String) Dim alphabet As New List(Of String) Dim charValue As Integer = CInt(IIf(upper, 65, 97)) For ctr As Integer = 0 To 25 alphabet.Add(ChrW(charValue + ctr).ToString()) Next Return alphabet End Function End Module ' The example displays the following output: ' MNOPQRSTUVWXYZ
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(Of T) object of type String, which it then passes to the Concat(IEnumerable(Of String)) method.
Imports System.Collections.Generic Module Example Public Sub Main() Dim maxPrime As Integer = 100 Dim primeList As IEnumerable(Of String) = GetPrimes(maxPrime) Console.WriteLine("Primes less than {0}:", maxPrime) Console.WriteLine(" {0}", String.Concat(primeList)) End Sub Private Function GetPrimes(maxPrime As Integer) As IEnumerable(Of String) Dim values As Array = Array.CreateInstance(GetType(Integer), _ New Integer() { maxPrime - 1}, New Integer(){ 2 }) ' Use Sieve of Erathsthenes to determine prime numbers. For ctr As Integer = values.GetLowerBound(0) To _ CInt(Math.Ceiling(Math.Sqrt(values.GetUpperBound(0)))) If CInt(values.GetValue(ctr)) = 1 Then Continue For For multiplier As Integer = ctr To maxPrime \ 2 If ctr * multiplier <= maxPrime Then values.SetValue(1, ctr * multiplier) Next Next Dim primes As New List(Of String) For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0) If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr.ToString() + " ") Next Return primes End Function End Module ' The example displays the following output: ' Primes less than 100: ' 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1