String.Join メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した配列の要素またはコレクションのメンバーを連結します。各要素の間には、指定した区切り記号が挿入されます。
オーバーロード
Join(Char, Object[]) |
各メンバー間に指定した区切り記号を使用して、オブジェクトの配列の文字列表現を連結します。 |
Join(Char, String[]) |
各メンバー間に指定した区切り記号を使用して、文字列の配列を連結します。 |
Join(String, IEnumerable<String>) |
IEnumerable<T> 型の構築された String コレクションのメンバーを連結します。各メンバーの間には、指定した区切り記号が挿入されます。 |
Join(String, Object[]) |
オブジェクト配列の要素を連結します。各要素の間には、指定した区切り記号が挿入されます。 |
Join(String, String[]) |
文字列配列のすべての要素を連結します。各要素の間には、指定した区切り記号が挿入されます。 |
Join(Char, String[], Int32, Int32) |
各メンバー間に指定した区切り記号を使用して、文字列の配列を連結します。 |
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
が 2 つ以上の要素を含む場合のみ、separator
が返される文字列に含まれます。
- values
- Object[]
文字列表現が連結されるオブジェクトの配列。
戻り値
separator
文字で区切られた、values
の要素で構成される文字列。
または
values
の要素がゼロの場合は Empty。
例外
value
が null
です。
結果として生成される文字列の長さが、許容される最大長 (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
が 2 つ以上の要素を含む場合のみ、separator
が返される文字列に含まれます。
- value
- String[]
連結する文字列の配列。
戻り値
separator
文字で区切られた、value
の要素で構成される文字列。
または
value
の要素がゼロの場合は Empty。
例外
value
が null
です。
結果として生成される文字列の長さが、許容される最大長 (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
区切り記号として使用する文字列。separator
が返される文字列に含まれるのは、values
が 2 つ以上の要素を含む場合に限られます。
- values
- IEnumerable<String>
連結する文字列を格納しているコレクション。
戻り値
values
の要素からなる、separator
文字列で区切られた文字列。
または
values
の要素がゼロの場合は Empty。
- 属性
例外
values
が null
です。
結果として生成される文字列の長さが、許容される最大長 (MaxValue) をオーバーフローしています。
例
次の例では、Eratosthenes の分割アルゴリズムを使用して、100 以下の素数を計算します。 結果を 型の オブジェクト List<T> に割り当て、その結果を String メソッドに渡 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
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
注釈
が separator
の null
場合は、代わりに空の文字列 ( String.Empty ) が使用されます。 のメンバーが values
の場合 null
は、代わりに空の文字列が使用されます。
Join(String, IEnumerable<String>) は、最初に要素を文字列配列に変換することなく、コレクション内の各要素を連結 IEnumerable(Of String)
できる便利なメソッドです。 これは、LINQ (Query) クエリLanguage-Integrated使用する場合に特に便利です。 次の例では、アルファベットの大文字または小文字を含む オブジェクトをラムダ式に渡します。この式では、特定の文字以上の文字を選択します (この例では List(Of String)
"M" です)。 メソッド IEnumerable(Of String)
によって返されるコレクションは Enumerable.Where 、 メソッドに渡され Join(String, IEnumerable<String>) 、結果が 1 つの文字列として表示されます。
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
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
が 2 つ以上の要素を含む場合のみ、separator
が返される文字列に含まれます。
- values
- Object[]
連結する要素を格納している配列。
戻り値
values
の要素からなる、separator
文字列で区切られた文字列。
または
values
の要素がゼロの場合は Empty。
または
.NET Framework のみ: values
の最初の要素が null
である場合は Empty。
- 属性
例外
values
が null
です。
結果として生成される文字列の長さが、許容される最大長 (MaxValue) をオーバーフローしています。
例
次の例では、Eratosthenes の分割アルゴリズムを使用して、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
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
注釈
が separator
の null
場合、または最初の要素以外の要素が の場合は、代わりに空の文字列 values
( ) null
String.Empty が使用されます。 の最初の要素が の場合は、「呼び出し元に関する注意事項」セクションを values
参照してください null
。
Join(String, Object[]) は、要素を文字列に明示的に変換することなく、オブジェクト配列内の各要素を連結できる便利なメソッドです。 配列内の各オブジェクトの文字列形式は、そのオブジェクトの メソッドを呼び出すことによって派生 ToString
します。
注意 (呼び出し元)
.NET Frameworkのみ: の最初の要素が の場合、 メソッドは 内の要素を連結するのではなく、 values
null
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
こちらもご覧ください
適用対象
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
が 2 つ以上の要素を含む場合のみ、separator
が返される文字列に含まれます。
- value
- String[]
連結する要素を格納している配列。
戻り値
value
内の要素からなる、separator
文字列で区切られた文字列。
または
values
の要素がゼロの場合は Empty。
例外
value
が null
です。
結果として生成される文字列の長さが、許容される最大長 (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
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
注釈
たとえば、 が ", " で、 の要素が separator
value
"apple"、"orange"、"orange"、"pear" の場合、 は Join(separator, value)
"apple, orange, orange, orange, pear" を返します。
が separator
の null
場合は、代わりに空の文字列 ( String.Empty ) が使用されます。 内の要素が value
の場合 null
は、代わりに空の文字列が使用されます。
こちらもご覧ください
適用対象
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
から始まる value
の count
要素で構成される文字列。
または
count
がゼロの場合は Empty。
例外
value
が null
です。
結果として生成される文字列の長さが、許容される最大長 (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
が 2 つ以上の要素を含む場合のみ、separator
が返される文字列に含まれます。
- value
- String[]
連結する要素を格納している配列。
- startIndex
- Int32
使用する value
の最初の要素。
- count
- Int32
使用する value
の要素の数。
戻り値
separator
文字で区切られた startIndex
から始まる value
の count
要素で構成される文字列。
または
count
がゼロの場合は Empty。
例外
value
が null
です。
startIndex
または count
が 0 未満です。
または
startIndex
と count
を加算すると、 value
にある要素の数を超えます。
メモリが不足しています。
例
次の例では、果物の名前の配列から 2 つの要素を連結します。
// 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'
注釈
たとえば、 が ", " で、 の要素が separator
value
"apple"、"orange"、"orange"、および "pear" の場合、 は Join(separator, value, 1, 2)
"orange, orange, orange" を返します。
が separator
の null
場合は、代わりに空の文字列 ( String.Empty ) が使用されます。 内の要素が value
の場合 null
は、代わりに空の文字列が使用されます。
こちらもご覧ください
適用対象
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
が 2 つ以上の要素を含む場合のみ、separator
が返される文字列に含まれます。
- values
- IEnumerable<T>
連結するオブジェクトを格納しているコレクション。
戻り値
separator
文字で区切られた values
のメンバーで構成される文字列。
または
values
の要素がない場合は Empty。
例外
values
が null
です。
結果として生成される文字列の長さが、許容される最大長 (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
が 2 つ以上の要素を含む場合のみ、separator
が返される文字列に含まれます。
- values
- IEnumerable<T>
連結するオブジェクトを格納しているコレクション。
戻り値
values
の要素からなる、separator
文字列で区切られた文字列。
または
values
の要素がない場合は Empty。
- 属性
例外
values
が null
です。
結果として生成される文字列の長さが、許容される最大長 (MaxValue) をオーバーフローしています。
例
次の例では、Eratosthenes の分割アルゴリズムを使用して、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
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
注釈
が separator
の null
場合は、代わりに空の文字列 ( String.Empty ) が使用されます。 のメンバーが values
の場合 null
は、代わりに空の文字列が使用されます。
Join<T>(String, IEnumerable<T>) は、コレクションの各メンバーを文字列に変換せずに連結できる便利 IEnumerable<T> なメソッドです。 コレクション内の各オブジェクトの文字列形式は IEnumerable<T> 、そのオブジェクトの メソッドを呼び出すことによって派生 ToString
します。
このメソッドは、クエリ (LINQ) Language-Integrated使用する場合に特に便利です。 たとえば、次のコードでは、動物の名前とそれが属する順序を含む非常に単純 Animal
なクラスを定義します。 次に、多数の List<T> オブジェクトを含む オブジェクトを定義 Animal
します。 拡張 Enumerable.Where メソッドを呼び出して、プロパティ Animal
が Order
"Rodent" と等しいオブジェクトを抽出します。 結果は メソッドに渡 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
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