在C#中, Join()是一个字符串方法。此方法用于在每个成员或元素之间使用指定的分隔符来连接集合的成员或指定数组的元素。可以通过向其传递不同的参数来重载此方法。 Join()方法的重载列表中共有5种方法,其中本文讨论了3种,其余2种将在C#中讨论。 Join()方法|套装– 2。
- String.Join(String,Obj [])
- String.Join(字符串,字符串[])
- String.Join(字符串,字符串[],int pos1,int pos2)
- String.Join(字符串,IEnumerable
) - String.Join
(String,IEnumerable )
String.Join(String,Obj [])
此方法用于在对象数组的每个元素之间使用分隔符来连接对象数组的元素。
句法:
public static string Join(string separator, params obj[] array)
- 参数:此方法有两个参数,其中之一是System.Sting类型的分隔符,用于根据用户的选择定义分隔符。另一个参数是System.Object []类型的数组,其中包含要串联的元素。
- 返回类型:此方法的返回类型为System.String 。
- 异常:如果数组为null,则此方法可以提供ArgumentNullException 。
示例:在下面的代码中,首先创建一个对象数组,然后将其与要使用的分隔符一起传递给join方法,这里使用’,’逗号分隔符,并在方法返回后,将字符串作为输出。
// C# program to demonstrate the
// Join(String, Obj [ ]) method
using System;
namespace ConsoleApplication1 {
class Geeks {
// Main Method
static void Main(string[] args)
{
// Creating an object array
// Here, It is consist of four
// elements only
object[] array = {"Hello", "Geeks", 12345, 786};
// Using Join method
// Here separator is ', '( comma )
string s1 = string.Join(", ", array);
// Finally after joining process gets over
// Getting the output of value of string s1
Console.WriteLine("Value of string s1 is " + s1);
}
}
}
输出:
Value of string s1 is Hello, Geeks, 12345, 786
String.Join(字符串,字符串[])
此方法用于在字符串数组的每个元素之间通过用户指定的分隔符来连接String数组的元素。
句法:
public static string Join(string separator, params string[ ] array)
- 参数:此方法有两个参数,其中一个是System.Sting类型的分隔符,用于根据用户的选择定义分隔符。另一个参数是System.String []类型的数组,其中包含要串联的元素。
- 返回类型:此方法的返回类型为System.String 。
- 异常:如果数组为null,则此方法可以提供ArgumentNullException 。
示例:在下面的代码中,首先创建一个字符串数组,并将其与要使用的分隔符一起传递给join方法,这里使用’/’斜杠分隔符,并在方法返回后将字符串作为输出打印。
// C# program to demonstrate the
// Join(String, String [])
using System;
namespace ConsoleApplication2 {
class Geeks {
// Main Method
static void Main(string[] args)
{
// Creating a string array
// Here It contains five
// elements only
string[] array = {"hello", " World ",
"Geeks", " are ", " here " };
// Using Join method
// Here separator used is '/'( slash )
string s1 = string.Join("/", array);
// Finally after join method
// Getting the output of value of string s1
Console.WriteLine("Value of string s1 is " + s1);
}
}
}
输出:
Value of string s1 is hello/ World /Geeks/ are / here
String.Join(字符串,字符串[],int pos1,int pos2)
此方法用于在数组的每个元素之间的用户定义的分隔符的帮助下,将String数组的元素连接到指定位置。
句法:
public static string Join(string separator, params string[] array, int pos1, int pos2)
- 参数:此方法有四个参数,第一个是System.Sting类型的分隔符,用于根据用户的选择定义分隔符。第二个参数是System.String []类型的数组,其中包含要串联的元素。第三个参数是System.Int32类型的pos1 ,它是数组中要使用的第一个元素,并且需要记住的重要一点是该参数使用了从零开始的索引。第四个参数是System.Int32类型的pos2 ,用于指定要使用的数组元素的数量,它始终从1开始,即它将pos1’的值作为第一个元素。
- 返回类型:此方法的返回类型为System.String 。如果pos2的值为零,它也可以返回String.Empty类型。
- 异常:如果数组为null,则此方法可以提供ArgumentNullException 。如果pos1或pos2小于零或pos1 + pos2大于数组中的元素数,则可以给出ArgumentOutOfRangeException ,并且此方法还可以给出OutOfMemoryException 。
示例:在下面的代码中,将创建一个字符串数组,并假设用户希望从位置索引2连接字符串,并希望覆盖五个元素。
// C# program to demonstrate the
// Join(String, string [ ], int
// pos1, int pos2) method
using System;
namespace ConsoleApplication3 {
class Geeks {
// Main Method
static void Main(string[] args)
{
// Creating a string array
string[] array = {"lazy", "dog", "jumps", "Over",
"the", "Lazy", "fox" };
// Using Join method
// Here separator used is '-'( hiphen )
// from index 2 and covers upto 5
// elements from index 2
string s1 = string.Join("-", array, 2, 5);
// Finally after joining process gets over
// Getting the output of value of string s1
Console.WriteLine("Value of string is " + s1);
}
}
}
输出:
Value of string is jumps-Over-the-Lazy-fox
参考:
- https://msdn.microsoft.com/zh-CN/library/tk0xe5h0(v=vs.110).aspx
- https://msdn.microsoft.com/zh-CN/library/57a79xd0(v=vs.110).aspx
- https://msdn.microsoft.com/zh-CN/library/tk0xe5h0(v=vs.110).aspx