String.Join 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
串連指定之陣列的項目或集合的成員,並在每個項目或成員之間使用指定的分隔符號。
多載
Join(String, String[]) |
串連字串陣列的所有項目,並在每個項目之間使用指定的分隔符號。 |
Join(String, ReadOnlySpan<String>) | |
Join(String, String[], Int32, Int32) |
串連字串陣列的指定項目,並在每個項目之間使用指定的分隔符號。 |
Join(Char, String[], Int32, Int32) |
在每個成員間使用所指定分隔符號來串連字串陣列,從位在 |
Join(String, ReadOnlySpan<Object>) | |
Join(Char, ReadOnlySpan<Object>) | |
Join(String, IEnumerable<String>) |
串連類型 IEnumerable<T> 之已建構的 String 集合的成員,並在每個成員之間使用指定的分隔符號。 |
Join(Char, Object[]) |
在每個成員間使用所指定分隔符號來串連物件陣列的字串表示。 |
Join(String, Object[]) |
串連物件陣列的項目,並在每個項目之間使用指定的分隔符號。 |
Join(Char, ReadOnlySpan<String>) | |
Join(Char, String[]) |
在每個成員間使用所指定分隔符號來串連字串陣列。 |
Join<T>(Char, IEnumerable<T>) |
串連集合的成員,並在每個成員之間使用指定的分隔符號。 |
Join<T>(String, IEnumerable<T>) |
串連集合的成員,並在每個成員之間使用指定的分隔符號。 |
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
字串分隔。
-或-
Empty 如果 values
有零個元素,則為 。
例外狀況
value
為 null
。
產生的字串長度會溢位 (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
備註
例如,如果 separator
是 “, ,而 的 value
元素是 ”apple“、”orange“、”orange“ 和 ”pear“, Join(separator, value)
則會傳回 ”apple, orange, grape, pear“。
如果 為 separator
null
,則會改用空字串 (String.Empty) 。 如果中的任何 value
專案為 null
,則會改用空字串。
另請參閱
適用於
Join(String, ReadOnlySpan<String>)
public:
static System::String ^ Join(System::String ^ separator, ReadOnlySpan<System::String ^> value);
public static string Join (string? separator, scoped ReadOnlySpan<string?> value);
static member Join : string * ReadOnlySpan<string> -> string
Public Shared Function Join (separator As String, value As ReadOnlySpan(Of String)) As String
參數
- separator
- String
- value
- ReadOnlySpan<String>
傳回
適用於
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
項目數目。
傳回
字串,由 count
以 字元分隔separator
的 開頭startIndex
的專案value
所組成。
-或-
Empty 如果 count
為零,則為 。
例外狀況
value
為 null
。
記憶體不足。
範例
下列範例會串連來自水果名稱數位的兩個專案。
// 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'
備註
例如,如果 separator
是 “, ,而 的 value
元素是 ”apple“、”orange“、”orange“、” 和 “pear”, Join(separator, value, 1, 2)
則會傳回 “orange, grape”。
如果 為 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
value
中要串連的元素數目,從 startIndex
位置中的元素開始。
傳回
字串,由 count
以 字元分隔separator
的 開頭startIndex
的專案value
所組成。
-或-
Empty 如果 count
為零,則為 。
例外狀況
value
為 null
。
產生的字串長度會溢位 (Int32.MaxValue) 的最大允許長度。
適用於
Join(String, ReadOnlySpan<Object>)
public:
static System::String ^ Join(System::String ^ separator, ReadOnlySpan<System::Object ^> values);
public static string Join (string? separator, scoped ReadOnlySpan<object?> values);
static member Join : string * ReadOnlySpan<obj> -> string
Public Shared Function Join (separator As String, values As ReadOnlySpan(Of Object)) As String
參數
- separator
- String
- values
- ReadOnlySpan<Object>
傳回
適用於
Join(Char, ReadOnlySpan<Object>)
public:
static System::String ^ Join(char separator, ReadOnlySpan<System::Object ^> values);
public static string Join (char separator, scoped ReadOnlySpan<object?> values);
static member Join : char * ReadOnlySpan<obj> -> string
Public Shared Function Join (separator As Char, values As ReadOnlySpan(Of Object)) As String
參數
- separator
- Char
- values
- ReadOnlySpan<Object>
傳回
適用於
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
字串分隔。
-或-
Empty 如果 values
有零個元素,則為 。
- 屬性
例外狀況
values
為 null
。
產生的字串長度會溢位 (Int32.MaxValue) 的最大允許長度。
範例
下列範例使用 Eratosthenes 演算法的 Sieve 來計算小於或等於 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
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
備註
如果 為 separator
null
,則會改用空字串 (String.Empty) 。 如果的任何成員 values
是 null
,則會改用空字串。
Join(String, IEnumerable<String>) 是方便的方法,可讓您串連集合中的每個 IEnumerable(Of String)
元素,而不需要先將元素轉換成字元串陣列。 Language-Integrated 查詢 (LINQ) 查詢表達式特別有用。 下列範例會將包含字母大寫或小寫字母的 對象傳遞 List(Of String)
至 Lambda 表達式,以選取等於或大於特定字母的字母 (,在此範例中為 “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(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
元素組成。
-或-
Empty 如果 values
有零個元素,則為 。
例外狀況
value
為 null
。
產生的字串長度會溢位 (Int32.MaxValue) 的最大允許長度。
適用於
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
字串分隔。
-或-
Empty 如果 values
有零個元素,則為 。
-或-
只有 .NET Framework:Empty如果的第一個專案values
是 null
。
- 屬性
例外狀況
values
為 null
。
產生的字串長度會溢位 (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
備註
如果 separator
是 null
或,如果第一個元素以外的任何元素 values
是 null
,則會改用空字串 (String.Empty) 。 如果的第一個專案 values
是 null
,請參閱呼叫端的附註一節。
Join(String, Object[]) 是方便的方法,可讓您串連物件數位中的每個元素,而不需明確地將其元素轉換成字串。 數位中每個物件的字串表示都是藉由呼叫該物件的 ToString
方法來衍生。
給呼叫者的注意事項
僅 .NET Framework:如果 的第一個專案values
是 ,則 Join(String, Object[]) 方法不會串連中的values
專案,而是傳回 Emptynull
。 有一些此問題的因應措施可供使用。 最簡單的方式是將 的值 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(Char, ReadOnlySpan<String>)
public:
static System::String ^ Join(char separator, ReadOnlySpan<System::String ^> value);
public static string Join (char separator, scoped ReadOnlySpan<string?> value);
static member Join : char * ReadOnlySpan<string> -> string
Public Shared Function Join (separator As Char, value As ReadOnlySpan(Of String)) As String
參數
- separator
- Char
- value
- ReadOnlySpan<String>
傳回
適用於
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
元素組成。
-或-
Empty 如果 value
有零個元素,則為 。
例外狀況
value
為 null
。
產生的字串長度會溢位 (Int32.MaxValue) 允許的最大長度。
適用於
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
成員組成。
-或-
Empty 如果 values
沒有專案,則為 。
例外狀況
values
為 null
。
產生的字串長度會溢位 (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
字串分隔。
-或-
Empty 如果 values
沒有專案,則為 。
- 屬性
例外狀況
values
為 null
。
產生的字串長度會溢位 (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
備註
如果 separator
為 null
,則會改用空字串 (String.Empty) 。 如果的任何成員 values
是 null
,則會改用空字串。
Join<T>(String, IEnumerable<T>) 是方便的方法,可讓您串連集合的每個 IEnumerable<T> 成員,而不需要先將它們轉換成字串。 集合中 IEnumerable<T> 每個物件的字串表示是藉由呼叫該物件的 ToString
方法來衍生。
這個方法特別適用於 Language-Integrated 查詢 (LINQ) 查詢表示式。 例如,下列程式代碼會定義一個非常簡單的 Animal
類別,其中包含動物的名稱及其所屬的順序。 然後,它會定義 List<T> 包含一些 Animal
對象的物件。 呼叫 Enumerable.Where 擴充方法,以擷取 Animal
其屬性等於 “Rodent” 的物件 Order
。 結果會傳遞至 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
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應