String.Join Yöntem

Tanım

Her öğe veya üye arasında belirtilen ayırıcıyı kullanarak belirtilen bir dizinin veya bir koleksiyonun üyelerinin öğelerini birleştirir.Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.

Aşırı Yüklemeler

Join(Char, Object[])

Her üye arasında belirtilen ayırıcıyı kullanarak bir nesne dizisinin dize gösterimlerini birleştirir.Concatenates the string representations of an array of objects, using the specified separator between each member.

Join(Char, String[])

Her üye arasında belirtilen ayırıcıyı kullanarak bir dize dizisini birleştirir.Concatenates an array of strings, using the specified separator between each member.

Join(String, IEnumerable<String>)

IEnumerable<T> String Her üye arasında belirtilen ayırıcıyı kullanarak, oluşturulmuş bir tür koleksiyonunun üyelerini birleştirir.Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member.

Join(String, Object[])

Her öğe arasında belirtilen ayırıcıyı kullanarak bir nesne dizisinin öğelerini birleştirir.Concatenates the elements of an object array, using the specified separator between each element.

Join(String, String[])

Her öğe arasında belirtilen ayırıcıyı kullanarak bir dize dizisinin tüm öğelerini birleştirir.Concatenates all the elements of a string array, using the specified separator between each element.

Join(Char, String[], Int32, Int32)

Her üye arasında belirtilen ayırıcıyı kullanarak, value konumda bulunan öğeden başlayarak startIndex ve öğeleri yukarı birleştirerek bir dizi dizeyi birleştirir count .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)

Her öğe arasında belirtilen ayırıcıyı kullanarak bir dize dizisinin belirtilen öğelerini birleştirir.Concatenates the specified elements of a string array, using the specified separator between each element.

Join<T>(Char, IEnumerable<T>)

Her üye arasındaki belirtilen ayırıcıyı kullanarak bir koleksiyonun üyelerini art arda ekler.Concatenates the members of a collection, using the specified separator between each member.

Join<T>(String, IEnumerable<T>)

Her üye arasındaki belirtilen ayırıcıyı kullanarak bir koleksiyonun üyelerini art arda ekler.Concatenates the members of a collection, using the specified separator between each member.

Join(Char, Object[])

Her üye arasında belirtilen ayırıcıyı kullanarak bir nesne dizisinin dize gösterimlerini birleştirir.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 <System::Object ^> ^ 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

Parametreler

separator
Char

Ayırıcı olarak kullanılacak karakter.The character to use as a separator. separator yalnızca birden fazla öğesi varsa döndürülen dizeye dahil edilir value .separator is included in the returned string only if value has more than one element.

values
Object[]

Dize temsilleri birleştirilecek bir nesne dizisi.An array of objects whose string representations will be concatenated.

Döndürülenler

String

Karakteri ile sınırlandırılmış öğelerinden oluşan bir dize values separator .A string that consists of the elements of values delimited by the separator character.

-veya--or-

Empty Eğer values sıfır öğe varsa.Empty if values has zero elements.

Özel durumlar

value, null değeridir.value is null.

Ortaya çıkan dizenin uzunluğu izin verilen uzunluk üst sınırını () taşıyor MaxValue .The length of the resulting string overflows the maximum allowed length (MaxValue).

Şunlara uygulanır

Join(Char, String[])

Her üye arasında belirtilen ayırıcıyı kullanarak bir dize dizisini birleştirir.Concatenates an array of strings, using the specified separator between each member.

public:
 static System::String ^ Join(char separator, ... cli::array <System::String ^> ^ 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

Parametreler

separator
Char

Ayırıcı olarak kullanılacak karakter.The character to use as a separator. separator yalnızca birden fazla öğesi varsa döndürülen dizeye dahil edilir value .separator is included in the returned string only if value has more than one element.

value
String[]

Birleştirilecek dizeler dizisi.An array of strings to concatenate.

Döndürülenler

String

Karakteri ile sınırlandırılmış öğelerinden oluşan bir dize value separator .A string that consists of the elements of value delimited by the separator character.

-veya--or-

Empty Eğer value sıfır öğe varsa.Empty if value has zero elements.

Özel durumlar

value, null değeridir.value is null.

Ortaya çıkan dizenin uzunluğu izin verilen uzunluk üst sınırını () taşıyor MaxValue .The length of the resulting string overflows the maximum allowed length (MaxValue).

Şunlara uygulanır

Join(String, IEnumerable<String>)

IEnumerable<T> String Her üye arasında belirtilen ayırıcıyı kullanarak, oluşturulmuş bir tür koleksiyonunun üyelerini birleştirir.Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member.

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

Parametreler

separator
String

Ayırıcı olarak kullanılacak dize.separatorThe string to use as a separator.separator yalnızca birden fazla öğesi varsa döndürülen dizeye dahil edilir values .is included in the returned string only if values has more than one element.

values
IEnumerable<String>

Birleştirilecek dizeleri içeren bir koleksiyon.A collection that contains the strings to concatenate.

Döndürülenler

String

Dize tarafından sınırlandırılmış öğelerinden oluşan bir dize values separator .A string that consists of the elements of values delimited by the separator string.

-veya--or-

Empty Eğer values sıfır öğe varsa.Empty if values has zero elements.

Öznitelikler

Özel durumlar

values, null değeridir.values is null.

Ortaya çıkan dizenin uzunluğu izin verilen uzunluk üst sınırını () taşıyor MaxValue .The length of the resulting string overflows the maximum allowed length (MaxValue).

Örnekler

Aşağıdaki örnekte, 100'e eşit veya daha küçük olan asal sayıları hesaplamak için Eratosthenes Süzgeci algoritması kullanılmaktadır.The following example uses the Sieve of Eratosthenes algorithm to calculate the prime numbers that are less than or equal to 100. Bu, sonucu bir nesnesine atar List<T> String ve ardından Join(String, IEnumerable<String>) yöntemine geçirilir.It assigns the result to a List<T> object of type String, which it then passes to the Join(String, IEnumerable<String>) method.

using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      int maxPrime = 100;
      List<int> primes = GetPrimes(maxPrime);
      Console.WriteLine("Primes less than {0}:", maxPrime);
      Console.WriteLine("   {0}", String.Join(" ", primes));
   }

   private static List<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);
      }      
      
      List<int> primes = new List<int>();
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         if ((int) values.GetValue(ctr) == 0) 
            primes.Add(ctr);
      return primes;
   }   
}
// 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
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim maxPrime As Integer = 100
      Dim primes As List(Of String) = GetPrimes(maxPrime)
      Console.WriteLine("Primes less than {0}:", maxPrime)
      Console.WriteLine("   {0}", String.Join(" ", primes))
   End Sub
   
   Private Function GetPrimes(maxPrime As Integer) As List(Of String)
      Dim values As Array = Array.CreateInstance(GetType(Integer), _
                              New Integer() { maxPrime - 1}, New Integer(){ 2 }) 
        ' Use Sieve of Eratosthenes 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

Açıklamalar

separatorİse null , bunun yerine boş bir dize ( String.Empty ) kullanılır.If separator is null, an empty string (String.Empty) is used instead. valuesÖğesi varsa null , bunun yerine boş bir dize kullanılır.If any member of values is null, an empty string is used instead.

Join(String, IEnumerable<String>) , IEnumerable(Of String) önce öğeleri bir dize dizisine dönüştürmeden bir koleksiyondaki her öğeyi birleştirme olanağı sağlayan bir kolaylık yöntemidir.Join(String, IEnumerable<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. Language-Integrated Query (LINQ) sorgu ifadelerinde özellikle yararlıdır.It is particularly useful with Language-Integrated Query (LINQ) query expressions. Aşağıdaki örnek, List(Of String) alfabeden büyük veya küçük harfli harfler içeren bir nesneyi belirli bir harfe eşit veya daha büyük harfler seçen bir lambda ifadesine geçirir (örnekte, "e" dır).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"). IEnumerable(Of String)Yöntemi tarafından döndürülen koleksiyon, Enumerable.Where Join(String, IEnumerable<String>) sonucu tek bir dize olarak göstermek için yöntemine geçirilir.The IEnumerable(Of String) collection returned by the Enumerable.Where method is passed to the Join(String, IEnumerable<String>) method to display the result as a single string.

using System;
using System.Collections.Generic;
using System.Linq;

public class Example
{
   public static void Main()
   {
      string output = String.Join(" ", GetAlphabet(true).Where( letter => 
                      letter.CompareTo("M") >= 0));
      Console.WriteLine(output);  
   }

   private static List<string> GetAlphabet(bool upper)
   {
      List<string> alphabet = new List<string>();
      int charValue = upper ? 65 : 97;
      for (int ctr = 0; ctr <= 25; ctr++)
         alphabet.Add(Convert.ToChar(charValue + ctr).ToString());
      return alphabet; 
   }
}
// The example displays the following output:
//      M N O P Q R S T U V W X Y Z
Imports System.Collections.Generic
Imports System.Linq

Module modMain
   Public Sub Main()
      Dim output As String = String.Join(" ", 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:
'      M N O P Q R S T U V W X Y Z

Ayrıca bkz.

Şunlara uygulanır

Join(String, Object[])

Her öğe arasında belirtilen ayırıcıyı kullanarak bir nesne dizisinin öğelerini birleştirir.Concatenates the elements of an object array, using the specified separator between each element.

public:
 static System::String ^ Join(System::String ^ separator, ... cli::array <System::Object ^> ^ values);
public static string Join (string separator, params object[] values);
public static string Join (string? separator, params object?[] values);
[System.Runtime.InteropServices.ComVisible(false)]
public static string Join (string separator, params object[] values);
static member Join : string * obj[] -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
static member Join : string * obj[] -> string
Public Shared Function Join (separator As String, ParamArray values As Object()) As String

Parametreler

separator
String

Ayırıcı olarak kullanılacak dize.The string to use as a separator. separator yalnızca birden fazla öğesi varsa döndürülen dizeye dahil edilir values .separator is included in the returned string only if values has more than one element.

values
Object[]

Birleştirilecek öğeleri içeren bir dizi.An array that contains the elements to concatenate.

Döndürülenler

String

Dize tarafından sınırlandırılmış öğelerinden oluşan bir dize values separator .A string that consists of the elements of values delimited by the separator string.

-veya--or-

Empty Eğer values sıfır öğe varsa.Empty if values has zero elements.

-veya--or-

Yalnızca .NET Framework: Empty ilk öğesi values null ..NET Framework only: Empty if the first element of values is null.

Öznitelikler

Özel durumlar

values, null değeridir.values is null.

Ortaya çıkan dizenin uzunluğu izin verilen uzunluk üst sınırını () taşıyor MaxValue .The length of the resulting string overflows the maximum allowed length (MaxValue).

Örnekler

Aşağıdaki örnekte, 100'e eşit veya daha küçük olan asal sayıları hesaplamak için Eratosthenes Süzgeci algoritması kullanılmaktadır.The following example uses the Sieve of Eratosthenes algorithm to calculate the prime numbers that are less than or equal to 100. Sonucu bir tamsayı dizisine atar ve ardından Join(String, Object[]) yöntemine geçirilir.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);
      }      
      
      List<int> primes = new List<int>();
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         if ((int) values.GetValue(ctr) == 0) 
            primes.Add(ctr);
      return primes.ToArray();
   }   
}
// 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
Module Example
   Public Sub Main()
      Dim maxPrime As Integer = 100
      Dim primes() As Integer = GetPrimes(maxPrime)
      Console.WriteLine("Primes less than {0}:", maxPrime)
      Console.WriteLine("   {0}", String.Join(" ", primes))
   End Sub
   
   Private Function GetPrimes(maxPrime As Integer) As Integer()
      Dim values As Array = Array.CreateInstance(GetType(Integer), _
                              New Integer() { maxPrime - 1}, New Integer(){ 2 }) 
        ' Use Sieve of Eratosthenes 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 System.Collections.Generic.List(Of Integer)
      For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
         If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr)
      Next            
      Return primes.ToArray()
   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

Açıklamalar

Varsa separator null veya ilk öğe dışında herhangi bir öğe ise, values null bunun yerine boş bir dize ( String.Empty ) kullanılır.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. İlk öğesi ise arayanlara ait Notlar bölümüne bakın values null .See the Notes for Callers section if the first element of values is null.

Join(String, Object[]) , öğeleri açıkça dizelere dönüştürmek zorunda kalmadan bir nesne dizisindeki her öğeyi birleştirme olanağı sağlayan bir kolaylık yöntemidir.Join(String, Object[]) is a convenience method that lets you concatenate each element in an object array without explicitly converting its elements to strings. Dizideki her nesnenin dize temsili, bu nesnenin yöntemi çağırarak türetilir ToString .The string representation of each object in the array is derived by calling that object's ToString method.

Arayanlara Notlar

Yalnızca .NET Framework: öğesinin ilk öğesi values ise null , Join(String, Object[]) yöntemi ' de öğeleri içine almaz, values ancak bunu döndürür Empty ..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. Bu sorun için bir dizi geçici çözüm kullanılabilir.A number of workarounds for this issue are available. EmptyAşağıdaki örnekte gösterildiği gibi, dizinin ilk öğesine bir değeri atamanız en kolay yoldur.The easiest is to assign a value of Empty to the first element of the array, as the following example shows.

[! Code-CSharp-InteractiveSystem. String. JOIN # 6] [! Code-vbSystem. String. JOIN # 6][!code-csharp-interactiveSystem.String.Join#6] [!code-vbSystem.String.Join#6]

Ayrıca bkz.

Şunlara uygulanır

Join(String, String[])

Her öğe arasında belirtilen ayırıcıyı kullanarak bir dize dizisinin tüm öğelerini birleştirir.Concatenates all the elements of a string array, using the specified separator between each element.

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

Parametreler

separator
String

Ayırıcı olarak kullanılacak dize.The string to use as a separator. separator yalnızca birden fazla öğesi varsa döndürülen dizeye dahil edilir value .separator is included in the returned string only if value has more than one element.

value
String[]

Birleştirilecek öğeleri içeren bir dizi.An array that contains the elements to concatenate.

Döndürülenler

String

Dize tarafından sınırlandırılmış olan öğelerden oluşan bir dize value separator .A string that consists of the elements in value delimited by the separator string.

-veya--or-

Empty Eğer values sıfır öğe varsa.Empty if values has zero elements.

Özel durumlar

value, null değeridir.value is null.

Ortaya çıkan dizenin uzunluğu izin verilen uzunluk üst sınırını () taşıyor MaxValue .The length of the resulting string overflows the maximum allowed length (MaxValue).

Örnekler

Aşağıdaki örnek Join yöntemini gösterir.The following example demonstrates the Join method.

using namespace System;
String^ MakeLine( int initVal, int multVal, String^ sep )
{
   array<String^>^sArr = gcnew array<String^>(10);
   for ( int i = initVal; i < initVal + 10; i++ )
      sArr[ i - initVal ] = String::Format( "{0, -3}", i * multVal );
   return String::Join( sep, sArr );
}

int main()
{
   Console::WriteLine( MakeLine( 0, 5, ", " ) );
   Console::WriteLine( MakeLine( 1, 6, "  " ) );
   Console::WriteLine( MakeLine( 9, 9, ": " ) );
   Console::WriteLine( MakeLine( 4, 7, "< " ) );
}
// The example displays the following output:
//       0  , 5  , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45
//       6    12   18   24   30   36   42   48   54   60
//       81 : 90 : 99 : 108: 117: 126: 135: 144: 153: 162
//       28 < 35 < 42 < 49 < 56 < 63 < 70 < 77 < 84 < 91
using System;

public class JoinTest
{
    public static void Main()
    {
        Console.WriteLine(MakeLine(0, 5, ", "));
        Console.WriteLine(MakeLine(1, 6, "  "));
        Console.WriteLine(MakeLine(9, 9, ": "));
        Console.WriteLine(MakeLine(4, 7, "< "));
    }

    private static string MakeLine(int initVal, int multVal, string sep)
    {
        string [] sArr = new string [10];

        for (int i = initVal; i < initVal + 10; i++)
            sArr[i - initVal] = String.Format("{0,-3}", i * multVal);

        return String.Join(sep, sArr);
    }
}
// The example displays the following output:
//       0  , 5  , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45
//       6    12   18   24   30   36   42   48   54   60
//       81 : 90 : 99 : 108: 117: 126: 135: 144: 153: 162
//       28 < 35 < 42 < 49 < 56 < 63 < 70 < 77 < 84 < 91
Public Class JoinTest
    
    Public Shared Sub Main()
        
        Console.WriteLine(MakeLine(0, 5, ", "))
        Console.WriteLine(MakeLine(1, 6, "  "))
        Console.WriteLine(MakeLine(9, 9, ": "))
        Console.WriteLine(MakeLine(4, 7, "< "))
    End Sub
    
    
    Private Shared Function MakeLine(initVal As Integer, multVal As Integer, sep As String) As String
        Dim sArr(10) As String
        Dim i As Integer
        
        
        For i = initVal To (initVal + 10) - 1
            sArr((i - initVal)) = [String].Format("{0,-3}", i * multVal)
        
        Next i
        Return [String].Join(sep, sArr)
    End Function 'MakeLine
End Class
' The example displays the following output:
'       0  , 5  , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45
'       6    12   18   24   30   36   42   48   54   60
'       81 : 90 : 99 : 108: 117: 126: 135: 144: 153: 162
'       28 < 35 < 42 < 49 < 56 < 63 < 70 < 77 < 84 < 91

Açıklamalar

Örneğin, separator "," ve öğeleri value "Apple", "turuncu", "Grape" ve "PEAR" ise Join(separator, value) "Apple, turuncu, Grape, PEAR" döndürür.For example, if separator is ", " and the elements of value are "apple", "orange", "grape", and "pear", Join(separator, value) returns "apple, orange, grape, pear".

separatorİse null , bunun yerine boş bir dize ( String.Empty ) kullanılır.If separator is null, an empty string (String.Empty) is used instead. Öğesinde varsa value null , bunun yerine boş bir dize kullanılır.If any element in value is null, an empty string is used instead.

Ayrıca bkz.

Şunlara uygulanır

Join(Char, String[], Int32, Int32)

Her üye arasında belirtilen ayırıcıyı kullanarak, value konumda bulunan öğeden başlayarak startIndex ve öğeleri yukarı birleştirerek bir dizi dizeyi birleştirir count .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.

public:
 static System::String ^ Join(char separator, cli::array <System::String ^> ^ value, int startIndex, int count);
public static string Join (char separator, string?[] value, int startIndex, int count);
public static string Join (char separator, string[] value, int startIndex, int count);
static member Join : char * string[] * int * int -> string
Public Shared Function Join (separator As Char, value As String(), startIndex As Integer, count As Integer) As String

Parametreler

separator
Char

Belirtilen dizinde bulunan öğeden başlayarak ve belirtilen sayıda öğe dahil olmak üzere, her üye arasındaki belirtilen ayırıcıyı kullanarak bir dizi dizeyi birleştirir.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.

value
String[]

Birleştirilecek dizeler dizisi.An array of strings to concatenate.

startIndex
Int32

Birleştirmek için içindeki ilk öğe value .The first item in value to concatenate.

count
Int32

valueKonumdaki öğeden başlayarak birleştirilecek öğe sayısı startIndex .The number of elements from value to concatenate, starting with the element in the startIndex position.

Döndürülenler

String

count value Karakteri ile ayrılmış olarak başlayan öğelerinden oluşan bir dize startIndex separator .A string that consists of count elements of value starting at startIndex delimited by the separator character.

-veya--or-

Emptycountsıfırsa.Empty if count is zero.

Özel durumlar

value, null değeridir.value is null.

startIndex veya count negatiftir.startIndex or count are negative.

-veya--or-

startIndex, uzunluğundan büyük value - count .startIndex is greater than the length of value - count.

Ortaya çıkan dizenin uzunluğu izin verilen uzunluk üst sınırını () taşıyor MaxValue .The length of the resulting string overflows the maximum allowed length (MaxValue).

Şunlara uygulanır

Join(String, String[], Int32, Int32)

Her öğe arasında belirtilen ayırıcıyı kullanarak bir dize dizisinin belirtilen öğelerini birleştirir.Concatenates the specified elements of a string array, using the specified separator between each element.

public:
 static System::String ^ Join(System::String ^ separator, cli::array <System::String ^> ^ value, int startIndex, int count);
public static string Join (string separator, string[] value, int startIndex, int count);
public static string Join (string? separator, string?[] value, int startIndex, int count);
static member Join : string * string[] * int * int -> string
Public Shared Function Join (separator As String, value As String(), startIndex As Integer, count As Integer) As String

Parametreler

separator
String

Ayırıcı olarak kullanılacak dize.The string to use as a separator. separator yalnızca birden fazla öğesi varsa döndürülen dizeye dahil edilir value .separator is included in the returned string only if value has more than one element.

value
String[]

Birleştirilecek öğeleri içeren bir dizi.An array that contains the elements to concatenate.

startIndex
Int32

Kullanılacak ilk öğe value .The first element in value to use.

count
Int32

Kullanılacak öğe sayısı value .The number of elements of value to use.

Döndürülenler

String

count value Karakteri ile ayrılmış olarak başlayan öğelerinden oluşan bir dize startIndex separator .A string that consists of count elements of value starting at startIndex delimited by the separator character.

-veya--or-

Emptycountsıfırsa.Empty if count is zero.

Özel durumlar

value, null değeridir.value is null.

startIndex veya count 0 ' dan küçük.startIndex or count is less than 0.

-veya--or-

startIndex Plus, count içindeki öğe sayısından daha büyük value .startIndex plus count is greater than the number of elements in value.

Bellek yetersiz.Out of memory.

Örnekler

Aşağıdaki örnek, meyve 'in bir ad dizisinden iki öğeyi birleştirir.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()
{
   array<String^>^val = {"apple","orange","grape","pear"};
   String^ sep = ", ";
   String^ result;
   Console::WriteLine( "sep = '{0}'", sep );
   Console::WriteLine( "val[] = {{'{0}' '{1}' '{2}' '{3}'}}", val[ 0 ], val[ 1 ], val[ 2 ], val[ 3 ] );
   result = String::Join( sep, val, 1, 2 );
   Console::WriteLine( "String::Join(sep, val, 1, 2) = '{0}'", result );
}

/*
This example produces the following results:
sep = ', '
val[] = {'apple' 'orange' 'grape' 'pear'}
String::Join(sep, val, 1, 2) = 'orange, grape'
*/
String[] val = {"apple", "orange", "grape", "pear"};
String sep   = ", ";
String result;

Console.WriteLine("sep = '{0}'", sep);
Console.WriteLine("val[] = {{'{0}' '{1}' '{2}' '{3}'}}", val[0], val[1], val[2], val[3]);
result = String.Join(sep, val, 1, 2);
Console.WriteLine("String.Join(sep, val, 1, 2) = '{0}'", result);

// This example produces the following results:
// sep = ', '
// val[] = {'apple' 'orange' 'grape' 'pear'}
// String.Join(sep, val, 1, 2) = 'orange, grape'
Class Sample
   Public Shared Sub Main()
      Dim val As [String]() =  {"apple", "orange", "grape", "pear"}
      Dim sep As [String] = ", "
      Dim result As [String]
      
      Console.WriteLine("sep = '{0}'", sep)
      Console.WriteLine("val() = {{'{0}' '{1}' '{2}' '{3}'}}", val(0), val(1), val(2), val(3))
      result = [String].Join(sep, val, 1, 2)
      Console.WriteLine("String.Join(sep, val, 1, 2) = '{0}'", result)
   End Sub
End Class 
'This example displays the following output:
'       sep = ', '
'       val() = {'apple' 'orange' 'grape' 'pear'}
'       String.Join(sep, val, 1, 2) = 'orange, grape'

Açıklamalar

Örneğin, separator "," ve öğeleri value "Apple", "turuncu", "Grape" ve "PEAR" ise Join(separator, value, 1, 2) "turuncu, Grape" döndürür.For example, if separator is ", " and the elements of value are "apple", "orange", "grape", and "pear", Join(separator, value, 1, 2) returns "orange, grape".

separatorİse null , bunun yerine boş bir dize ( String.Empty ) kullanılır.If separator is null, an empty string (String.Empty) is used instead. Öğesinde varsa value null , bunun yerine boş bir dize kullanılır.If any element in value is null, an empty string is used instead.

Ayrıca bkz.

Şunlara uygulanır

Join<T>(Char, IEnumerable<T>)

Her üye arasındaki belirtilen ayırıcıyı kullanarak bir koleksiyonun üyelerini art arda ekler.Concatenates the members of a collection, using the specified separator between each member.

public:
generic <typename T>
 static System::String ^ Join(char separator, System::Collections::Generic::IEnumerable<T> ^ values);
public static string Join<T> (char separator, System.Collections.Generic.IEnumerable<T> values);
static member Join : char * seq<'T> -> string
Public Shared Function Join(Of T) (separator As Char, values As IEnumerable(Of T)) As String

Tür Parametreleri

T

Üyelerinin türü values .The type of the members of values.

Parametreler

separator
Char

Ayırıcı olarak kullanılacak karakter.The character to use as a separator. separator yalnızca birden fazla öğesi varsa döndürülen dizeye dahil edilir values .separator is included in the returned string only if values has more than one element.

values
IEnumerable<T>

Birleştirilecek nesneleri içeren bir koleksiyon.A collection that contains the objects to concatenate.

Döndürülenler

String

Karakteri ile ayrılmış üyelerinden oluşan bir dize values separator .A string that consists of the members of values delimited by the separator character.

-veya--or-

Emptyvaluesöğesi yoksa.Empty if values has no elements.

Özel durumlar

values, null değeridir.values is null.

Ortaya çıkan dizenin uzunluğu izin verilen uzunluk üst sınırını () taşıyor MaxValue .The length of the resulting string overflows the maximum allowed length (MaxValue).

Şunlara uygulanır

Join<T>(String, IEnumerable<T>)

Her üye arasındaki belirtilen ayırıcıyı kullanarak bir koleksiyonun üyelerini art arda ekler.Concatenates the members of a collection, using the specified separator between each member.

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

Tür Parametreleri

T

Üyelerinin türü values .The type of the members of values.

Parametreler

separator
String

Ayırıcı olarak kullanılacak dize.The string to use as a separator. separator yalnızca birden fazla öğesi varsa döndürülen dizeye dahil edilir values .separator is included in the returned string only if values has more than one element.

values
IEnumerable<T>

Birleştirilecek nesneleri içeren bir koleksiyon.A collection that contains the objects to concatenate.

Döndürülenler

String

Dize tarafından sınırlandırılmış öğelerinden oluşan bir dize values separator .A string that consists of the elements of values delimited by the separator string.

-veya--or-

Emptyvaluesöğesi yoksa.Empty if values has no elements.

Öznitelikler

Özel durumlar

values, null değeridir.values is null.

Ortaya çıkan dizenin uzunluğu izin verilen uzunluk üst sınırını () taşıyor MaxValue .The length of the resulting string overflows the maximum allowed length (MaxValue).

Örnekler

Aşağıdaki örnekte, 100'e eşit veya daha küçük olan asal sayıları hesaplamak için Eratosthenes Süzgeci algoritması kullanılmaktadır.The following example uses the Sieve of Eratosthenes algorithm to calculate the prime numbers that are less than or equal to 100. Bu, sonucu List<T> tamsayı türünde bir nesneye atar ve ardından Join<T>(String, IEnumerable<T>) yöntemine geçirilir.It assigns the result to a List<T> object of type integer, which it then passes to the Join<T>(String, IEnumerable<T>) method.

using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      int maxPrime = 100;
      List<int> primes = GetPrimes(maxPrime);
      Console.WriteLine("Primes less than {0}:", maxPrime);
      Console.WriteLine("   {0}", String.Join(" ", primes));
   }

   private static List<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);
      }      
      
      List<int> primes = new List<int>();
      for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
         if ((int) values.GetValue(ctr) == 0) 
            primes.Add(ctr);
      return primes;
   }   
}
// 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
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim maxPrime As Integer = 100
      Dim primes As List(Of Integer) = GetPrimes(maxPrime)
      Console.WriteLine("Primes less than {0}:", maxPrime)
      Console.WriteLine("   {0}", String.Join(" ", primes))
   End Sub
   
   Private Function GetPrimes(maxPrime As Integer) As List(Of Integer)
      Dim values As Array = Array.CreateInstance(GetType(Integer), _
                              New Integer() { maxPrime - 1}, New Integer(){ 2 }) 
        ' Use Sieve of Eratosthenes 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 System.Collections.Generic.List(Of Integer)
      For ctr As Integer = values.GetLowerBound(0) To values.GetUpperBound(0)
         If CInt(values.GetValue(ctr)) = 0 Then primes.Add(ctr)
      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

Açıklamalar

separatorİse null , bunun yerine boş bir dize ( String.Empty ) kullanılır.If separator is null, an empty string (String.Empty) is used instead. valuesÖğesi varsa null , bunun yerine boş bir dize kullanılır.If any member of values is null, an empty string is used instead.

Join<T>(String, IEnumerable<T>) , bir koleksiyonun her üyesini, IEnumerable<T> önce bunları dizelere dönüştürmeden bir araya aktarmanıza imkan tanıyan kolaylık bir yöntemdir.Join<T>(String, IEnumerable<T>) is a convenience method that lets you concatenate each member of an IEnumerable<T> collection without first converting them to strings. Koleksiyondaki her nesnenin dize temsili, IEnumerable<T> Bu nesnenin yöntemi çağırarak türetilir ToString .The string representation of each object in the IEnumerable<T> collection is derived by calling that object's ToString method.

Bu yöntem Language-Integrated Query (LINQ) sorgu ifadelerinde yararlı olur.This method is particular useful with Language-Integrated Query (LINQ) query expressions. Örneğin, aşağıdaki kod Animal bir hayvan adının ve ait olduğu sıranın adını içeren çok basit bir sınıfı tanımlar.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. Daha sonra List<T> çok sayıda nesne içeren bir nesnesi tanımlar Animal .It then defines a List<T> object that contains a number of Animal objects. Enumerable.Where Animal Order Özelliği "Rodent" değerine eşit olan nesneleri ayıklamak için genişletme yöntemi çağırılır.The Enumerable.Where extension method is called to extract the Animal objects whose Order property equals "Rodent". Sonuç Join<T>(String, IEnumerable<T>) yöntemine geçirilir.The result is passed to the Join<T>(String, IEnumerable<T>) method.

using System;
using System.Collections.Generic;
using System.Linq;

public class Animal
{
   public string Kind;
   public string Order;
   
   public Animal(string kind, string order)
   {
      this.Kind = kind;
      this.Order = order;
   }
   
   public override string ToString()
   {
      return this.Kind;
   }
}

public class Example
{
   public static void Main()
   {
      List<Animal> animals = new List<Animal>();
      animals.Add(new Animal("Squirrel", "Rodent"));
      animals.Add(new Animal("Gray Wolf", "Carnivora"));
      animals.Add(new Animal("Capybara", "Rodent"));
      string output = String.Join(" ", animals.Where( animal => 
                      (animal.Order == "Rodent")));
      Console.WriteLine(output);  
   }
}
// The example displays the following output:
//      Squirrel Capybara
Imports System.Collections.Generic

Public Class Animal
   Public Kind As String
   Public Order As String
   
   Public Sub New(kind As String, order As String)
      Me.Kind = kind
      Me.Order = order
   End Sub
   
   Public Overrides Function ToString() As String
      Return Me.Kind
   End Function
End Class

Module Example
   Public Sub Main()
      Dim animals As New List(Of Animal)
      animals.Add(New Animal("Squirrel", "Rodent"))
      animals.Add(New Animal("Gray Wolf", "Carnivora"))
      animals.Add(New Animal("Capybara", "Rodent")) 
      Dim output As String = String.Join(" ", animals.Where(Function(animal) _
                                           animal.Order = "Rodent"))
      Console.WriteLine(output)                                           
   End Sub
End Module
' The example displays the following output:
'      Squirrel Capybara

Ayrıca bkz.

Şunlara uygulanır