String.Join 메서드

정의

각 요소 또는 멤버 사이에 지정된 구분 기호를 사용하여 지정된 배열 요소나 컬렉션 멤버를 연결합니다.

오버로드

Join(Char, Object[])

각 멤버 사이에 지정된 구분 기호를 사용하여 개체 배열의 문자열 표현을 연결합니다.

Join(Char, String[])

각 멤버 사이에 지정된 구분 기호를 사용하여 문자열의 배열을 연결합니다.

Join(String, IEnumerable<String>)

각 멤버 사이에 지정된 구분 기호를 사용하여 IEnumerable<T> 형식의 생성된 String 컬렉션의 멤버를 연결합니다.

Join(String, Object[])

각 요소 사이에 지정된 구분 기호를 사용하여 개체 배열의 요소를 연결합니다.

Join(String, String[])

각 요소 사이에 지정된 구분 기호를 사용하여 문자열 배열의 모든 요소를 연결합니다.

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

각 멤버 사이에 지정된 구분 기호를 사용하고 startIndex 위치에 있는 value의 요소부터 최대 count개 요소를 연결하여 문자열의 배열을 연결합니다.

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

각 요소 사이에 지정된 구분 기호를 사용하여 문자열 배열의 지정된 요소를 연결합니다.

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

각 멤버 사이에 지정된 구분 기호를 사용하여 컬렉션의 멤버를 연결합니다.

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

각 멤버 사이에 지정된 구분 기호를 사용하여 컬렉션의 멤버를 연결합니다.

Join(Char, Object[])

각 멤버 사이에 지정된 구분 기호를 사용하여 개체 배열의 문자열 표현을 연결합니다.

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

매개 변수

separator
Char

구분 기호로 사용할 문자입니다. value에 둘 이상의 요소가 있는 경우에만 separator가 반환된 문자열에 포함됩니다.

values
Object[]

연결할 문자열 표현을 포함하는 개체의 배열입니다.

반환

separator 문자로 구분된 values의 요소로 구성된 문자열입니다.

또는

values에 요소가 없는 경우 Empty입니다.

예외

valuenull입니다.

결과 문자열의 길이는 허용되는 최대 길이(Int32.MaxValue)를 오버플로합니다.

적용 대상

Join(Char, String[])

각 멤버 사이에 지정된 구분 기호를 사용하여 문자열의 배열을 연결합니다.

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

매개 변수

separator
Char

구분 기호로 사용할 문자입니다. value에 둘 이상의 요소가 있는 경우에만 separator가 반환된 문자열에 포함됩니다.

value
String[]

연결할 문자열의 배열입니다.

반환

separator 문자로 구분된 value의 요소로 구성된 문자열입니다.

또는

value에 요소가 없는 경우 Empty입니다.

예외

valuenull입니다.

결과 문자열의 길이는 허용되는 최대 길이(Int32.MaxValue)를 오버플로합니다.

적용 대상

Join(String, IEnumerable<String>)

각 멤버 사이에 지정된 구분 기호를 사용하여 IEnumerable<T> 형식의 생성된 String 컬렉션의 멤버를 연결합니다.

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

매개 변수

separator
String

구분 기호로 사용할 문자열입니다.values에 둘 이상의 요소가 있는 경우에만 separator가 반환된 문자열에 포함됩니다.

values
IEnumerable<String>

연결할 문자열을 포함하는 컬렉션입니다.

반환

values 문자열로 구분된 separator의 요소로 구성된 문자열입니다.

또는

values에 요소가 없는 경우 Empty입니다.

특성

예외

valuesnull입니다.

결과 문자열의 길이는 허용되는 최대 길이(Int32.MaxValue)를 오버플로합니다.

예제

다음 예제에서는 Eratosthenes의 Sieve 알고리즘을 사용하여 100보다 작거나 같은 소수를 계산합니다. 결과를 형식String의 개체에 List<T> 할당한 다음 메서드에 Join(String, IEnumerable<String>) 전달합니다.

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
open System

let getPrimes maxPrime =
    let values = Array.CreateInstance(typeof<int>, [| maxPrime - 1 |], [| 2 |])
    // Use Sieve of Eratosthenes to determine prime numbers.
    for i = values.GetLowerBound 0 to values.GetUpperBound 0 |> float |> sqrt |> ceil |> int do
        if values.GetValue i :?> int <> 1 then
            for multiplier = i to maxPrime / 2 do
                if i * multiplier <= maxPrime then
                    values.SetValue(1, i * multiplier)

    let primes = ResizeArray()
    for i = values.GetLowerBound 0 to values.GetUpperBound 0 do
        if values.GetValue i :?> int = 0 then
            primes.Add i
    primes

let maxPrime = 100
let primes = getPrimes maxPrime
printfn $"Primes less than {maxPrime}:"
printfn $"""   {String.Join(" ", 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

설명

가 이nullseparator 빈 문자열(String.Empty)이 대신 사용됩니다. 의 values 멤버가 이 null면 빈 문자열이 대신 사용됩니다.

Join(String, IEnumerable<String>) 는 요소를 문자열 배열로 먼저 변환하지 않고 컬렉션의 IEnumerable(Of String) 각 요소를 연결할 수 있는 편리한 메서드입니다. LINQ(Language-Integrated Query) 쿼리 식에 특히 유용합니다. 다음 예제에서는 알파벳의 대문자나 소문자가 포함된 개체를 특정 문자와 같거나 큰 문자를 선택하는 람다 식에 전달 List(Of String) 합니다(예에서는 "M"). IEnumerable(Of String) 메서드에서 Enumerable.Where 반환된 Join(String, IEnumerable<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(((char)(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
// This F# example uses Seq.filter instead of Linq.
open System

let getAlphabet upper =
    let charValue = if upper then 65 else 97
    seq {
        for i = 0 to 25 do
            charValue + i
            |> char
            |> string
    }

String.Join(" ", getAlphabet true |> Seq.filter (fun letter -> letter.CompareTo "M" >= 0))
|> printfn "%s"

// 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

추가 정보

적용 대상

Join(String, Object[])

각 요소 사이에 지정된 구분 기호를 사용하여 개체 배열의 요소를 연결합니다.

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

매개 변수

separator
String

구분 기호로 사용할 문자열입니다. values에 둘 이상의 요소가 있는 경우에만 separator가 반환된 문자열에 포함됩니다.

values
Object[]

연결할 요소가 포함된 배열입니다.

반환

values 문자열로 구분된 separator의 요소로 구성된 문자열입니다.

또는

values에 요소가 없는 경우 Empty입니다.

또는

.NET Framework만: values의 첫 번째 요소가 null인 경우 Empty입니다.

특성

예외

valuesnull입니다.

결과 문자열의 길이는 허용되는 최대 길이(Int32.MaxValue)를 오버플로합니다.

예제

다음 예제에서는 Eratosthenes의 Sieve 알고리즘을 사용하여 100보다 작거나 같은 소수를 계산합니다. 정수 배열에 결과를 할당한 다음 메서드에 Join(String, Object[]) 전달합니다.

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
open System

let getPrimes maxPrime =
    let values = Array.CreateInstance(typeof<int>, [| maxPrime - 1 |], [| 2 |])
    // Use Sieve of Eratosthenes to determine prime numbers.
    for i = values.GetLowerBound 0 to values.GetUpperBound 0 |> float |> sqrt |> ceil |> int do
        if values.GetValue i :?> int <> 1 then
            for multiplier = i to maxPrime / 2 do
                if i * multiplier <= maxPrime then
                    values.SetValue(1, i * multiplier)

    [| for i = values.GetLowerBound 0 to values.GetUpperBound 0 do
        if values.GetValue i :?> int = 0 then
            i |]

let maxPrime = 100
let primes = getPrimes maxPrime
printfn $"Primes less than {maxPrime}:"
printfn $"""   {String.Join(" ", 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
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

설명

null 이거나 첫 번째 요소 이외의 요소가 valuesnullseparator 빈 문자열(String.Empty)이 대신 사용됩니다. 의 첫 번째 요소가 인 경우 호출자에 대한 참고 섹션을 valuesnull참조하세요.

Join(String, Object[]) 는 요소를 문자열로 명시적으로 변환하지 않고 개체 배열의 각 요소를 연결할 수 있는 편리한 메서드입니다. 배열에 있는 각 개체의 문자열 표현은 해당 개체의 ToString 메서드를 호출하여 파생됩니다.

호출자 참고

.NET Framework 전용: 의 valuesnull첫 번째 요소가 인 Join(String, Object[]) 경우 메서드는 의 values 요소를 연결하지 않고 대신 를 반환합니다Empty. 이 문제에 대한 여러 가지 해결 방법을 사용할 수 있습니다. 가장 쉬운 방법은 다음 예제와 같이 배열의 첫 번째 요소에 값을 Empty 할당하는 것입니다.

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
let values: obj[] = [| null; "Cobb"; 4189; 11434; 0.366 |]
if values[0] = null then 
   values[0] <- String.Empty
printfn $"""{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.366

추가 정보

적용 대상

Join(String, String[])

각 요소 사이에 지정된 구분 기호를 사용하여 문자열 배열의 모든 요소를 연결합니다.

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

매개 변수

separator
String

구분 기호로 사용할 문자열입니다. value에 둘 이상의 요소가 있는 경우에만 separator가 반환된 문자열에 포함됩니다.

value
String[]

연결할 요소가 포함된 배열입니다.

반환

value 문자열로 구분된 separator의 요소로 구성된 문자열입니다.

또는

values에 요소가 없는 경우 Empty입니다.

예외

valuenull입니다.

결과 문자열의 길이는 허용되는 최대 길이(Int32.MaxValue)를 오버플로합니다.

예제

다음 예제는 Join 메서드.

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
open System

let makeLine initVal multVal (sep: string) =
    let sArr = Array.zeroCreate<string> 10 

    for i = initVal to initVal + 9 do
        sArr[i - initVal] <- String.Format("{0,-3}", i * multVal)

    String.Join(sep, sArr)

printfn $"""{makeLine 0 5 ", "}"""
printfn $"""{makeLine 1 6 "  "}"""
printfn $"""{makeLine 9 9 ": "}"""
printfn $"""{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
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

설명

예를 들어 가 ", "이고 의 value 요소가 "apple", "orange", "grape", "pear" Join(separator, value) 이면 separator "apple, orange, grape, pear"를 반환합니다.

가 이nullseparator 빈 문자열(String.Empty)이 대신 사용됩니다. 의 요소가 이 valuenull면 빈 문자열이 대신 사용됩니다.

추가 정보

적용 대상

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

각 멤버 사이에 지정된 구분 기호를 사용하고 startIndex 위치에 있는 value의 요소부터 최대 count개 요소를 연결하여 문자열의 배열을 연결합니다.

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

매개 변수

separator
Char

각 멤버 사이에 지정된 구분 기호를 사용하고 지정된 인덱스에 있는 요소부터 지정된 수의 요소를 포함하여 문자열의 배열을 연결합니다.

value
String[]

연결할 문자열의 배열입니다.

startIndex
Int32

연결할 value의 첫 번째 항목입니다.

count
Int32

startIndex 위치의 요소에서 시작하여 연결할 value의 요소 수입니다.

반환

separator 문자로 구분된 startIndex로 시작하는 valuecount 요소로 구성된 문자열입니다.

또는

count가 0인 경우 Empty입니다.

예외

valuenull입니다.

startIndex 또는 count가 음수인 경우

또는

startIndexvalue - count의 길이보다 큽니다.

결과 문자열의 길이는 허용되는 최대 길이(Int32.MaxValue)를 오버플로합니다.

적용 대상

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

각 요소 사이에 지정된 구분 기호를 사용하여 문자열 배열의 지정된 요소를 연결합니다.

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

매개 변수

separator
String

구분 기호로 사용할 문자열입니다. value에 둘 이상의 요소가 있는 경우에만 separator가 반환된 문자열에 포함됩니다.

value
String[]

연결할 요소가 포함된 배열입니다.

startIndex
Int32

사용할 value의 첫 번째 요소입니다.

count
Int32

value 중에서 사용할 요소의 수입니다.

반환

separator 문자로 구분된 startIndex로 시작하는 valuecount 요소로 구성된 문자열입니다.

또는

count가 0인 경우 Empty입니다.

예외

valuenull입니다.

startIndex 또는 count 가 0 미만입니다.

또는

startIndex + countvalue의 요소 수보다 큽니다.

메모리가 부족합니다.

예제

다음 예제에서는 과일 이름 배열의 두 요소를 연결합니다.

// 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'
open System

let vals = [| "apple"; "orange"; "grape"; "pear" |]
let sep   = ", "

printfn $"sep = '{sep}'"
printfn $"vals[] = {{'{vals[0]}' '{vals[1]}' '{vals[2]}' '{vals[3]}'}}"
let result = String.Join(sep, vals, 1, 2)
printfn $"String.Join(sep, vals, 1, 2) = '{result}'"

// This example produces the following results:
// sep = ', '
// vals[] = {'apple' 'orange' 'grape' 'pear'}
// String.Join(sep, vals, 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'

설명

예를 들어 가 ", "이고 의 value 요소가 "apple", "orange", "grape", "pear" Join(separator, value, 1, 2) 인 경우 separator 는 "orange, grape"를 반환합니다.

가 이nullseparator 빈 문자열(String.Empty)이 대신 사용됩니다. 의 요소가 이 valuenull면 빈 문자열이 대신 사용됩니다.

추가 정보

적용 대상

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

각 멤버 사이에 지정된 구분 기호를 사용하여 컬렉션의 멤버를 연결합니다.

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

values 멤버의 형식입니다.

매개 변수

separator
Char

구분 기호로 사용할 문자입니다. values에 둘 이상의 요소가 있는 경우에만 separator가 반환된 문자열에 포함됩니다.

values
IEnumerable<T>

연결할 개체를 포함하는 컬렉션입니다.

반환

separator 문자열로 구분된 values의 멤버로 구성된 문자열입니다.

또는

values에 요소가 없는 경우 Empty입니다.

예외

valuesnull입니다.

결과 문자열의 길이는 허용되는 최대 길이(Int32.MaxValue)를 오버플로합니다.

적용 대상

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

각 멤버 사이에 지정된 구분 기호를 사용하여 컬렉션의 멤버를 연결합니다.

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

values 멤버의 형식입니다.

매개 변수

separator
String

구분 기호로 사용할 문자열입니다. values에 둘 이상의 요소가 있는 경우에만 separator가 반환된 문자열에 포함됩니다.

values
IEnumerable<T>

연결할 개체를 포함하는 컬렉션입니다.

반환

values 문자열로 구분된 separator의 요소로 구성된 문자열입니다.

또는

values에 요소가 없는 경우 Empty입니다.

특성

예외

valuesnull입니다.

결과 문자열의 길이는 허용되는 최대 길이(Int32.MaxValue)를 오버플로합니다.

예제

다음 예제에서는 Eratosthenes의 Sieve 알고리즘을 사용하여 100보다 작거나 같은 소수를 계산합니다. 결과를 List<T> 정수 형식의 개체에 할당한 다음 메서드에 Join<T>(String, IEnumerable<T>) 전달합니다.

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
open System

let getPrimes maxPrime =
    let values = Array.CreateInstance(typeof<int>, [| maxPrime - 1 |], [| 2 |])
    // Use Sieve of Eratosthenes to determine prime numbers.
    for i = values.GetLowerBound 0 to values.GetUpperBound 0 |> float |> sqrt |> ceil |> int do
        if values.GetValue i <> 1 then
            for multiplier = i to maxPrime / 2 do
                if i * multiplier <= maxPrime then
                    values.SetValue(1, i * multiplier)

    let primes = ResizeArray()
    for i = values.GetLowerBound 0 to values.GetUpperBound 0 do
        if values.GetValue i :?> int = 0 then
            primes.Add i
    primes

let maxPrime = 100
let primes = getPrimes maxPrime
printfn $"Primes less than {maxPrime}:"
printfn $"""   {String.Join(" ", 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

설명

가 이nullseparator 빈 문자열(String.Empty)이 대신 사용됩니다. 의 values 멤버가 이 null면 빈 문자열이 대신 사용됩니다.

Join<T>(String, IEnumerable<T>) 은 먼저 문자열로 변환하지 않고 컬렉션의 각 멤버를 IEnumerable<T> 연결할 수 있는 편리한 메서드입니다. 컬렉션에 있는 IEnumerable<T> 각 개체의 문자열 표현은 해당 개체의 ToString 메서드를 호출하여 파생됩니다.

이 메서드는 LINQ(Language-Integrated Query) 쿼리 식에 특히 유용합니다. 예를 들어 다음 코드는 동물의 이름과 동물 이름이 속한 순서를 포함하는 매우 간단한 Animal 클래스를 정의합니다. 그런 다음, 여러 Animal 개체를 List<T> 포함하는 개체를 정의합니다. Enumerable.Where 확장 메서드는 속성이 "Rodent"와 같은 개체를 Order 추출 Animal 하기 위해 호출됩니다. 결과는 메서드에 Join<T>(String, IEnumerable<T>) 전달됩니다.

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
// This example uses F#'s Seq.filter function instead of Linq.
open System

type Animal =
  { Kind: string
    Order: string }
    override this.ToString() =
        this.Kind

let animals = ResizeArray()
animals.Add { Kind = "Squirrel"; Order = "Rodent" }
animals.Add { Kind = "Gray Wolf"; Order = "Carnivora" }
animals.Add { Kind = "Capybara"; Order = "Rodent" }
String.Join(" ", animals |> Seq.filter (fun animal -> animal.Order = "Rodent"))
|> printfn "%s"
// 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

추가 정보

적용 대상