在C#中, 加入 是一个字符串方法。此方法用于使用每个成员或元素之间的指定分隔符连接集合的成员或指定数组的元素。通过向该方法传递不同的参数,可以重载该方法。关于前三种方法,请参阅 C#Set-1中的Join()方法 .
- 一串Join(字符串,Obj[])
- 一串连接(字符串,字符串[])
- 一串Join(String,String[],int-pos1,int-pos2)
- 一串Join(String,IEnumerable
) - 一串Join
(字符串,IEnumerable )
一串Join(String,IEnumerable )
此方法用于在每个成员之间使用指定的分隔符连接String类型的构造集合的成员。
语法:
public static string Join(string separator, IEnumerable L1)
参数:
分离器 :它是用作分隔符的字符串。仅当值包含多个元素且类型为时,返回的字符串中才会包含分隔符 系统一串 .
L1 :它是一个集合,包含要连接的字符串,其类型为 系统收藏。通用的IEnumerable
.
返回类型: 此方法返回类型为的字符串 系统一串 它由分隔符字符串分隔的值的成员组成。如果值没有成员,则该方法返回 一串空的 .
例外情况: 这种方法可以提供 无理例外 如果L1为空。
例子: 在下面的程序中 词表 使用内置列表集合创建。所以 同一个列表集合 与join()方法中的分隔符一起传递,因此,用户获得结果字符串。
// C# program to demonstrate the // Join(String, IEnumerable <string> L1 ) using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Geeks { // Main Method static void Main( string [] args) { // getting the added words // from list collections // and copying them into // another object of list type List<String> alpha = AddWords(); // passing the object of list // type along with the separator string str1 = string .Join( "--" , alpha); // getting the value of the string.. Console.WriteLine( "The value of the string is " + str1); } // creating a collection of // string values using List private static List<String> AddWords() { List<String> alpha = new List< string >(); // methods to add a string into list alpha.Add( "Hello" ); alpha.Add( "Geeks" ); alpha.Add( "How" ); alpha.Add( "are" ); alpha.Add( "you?" ); // returning the object // of the list type... return alpha; } } } |
The value of the string is Hello--Geeks--How--are--you?
一串Join (字符串,IEnumerable )
此方法用于在每个成员之间使用指定的分隔符,连接任何用户定义的数据类型(例如T)的构造集合的成员。
语法:
public static string Join(string separator, IEnumerable T1)
参数:
分离器 :它是用作分隔符的字符串。仅当值包含多个元素且类型为时,返回的字符串中才会包含分隔符 系统一串 .
T1 :它是一个集合,包含要连接的对象,其类型为 系统收藏。通用的IEnumerable
.
返回类型: 此方法返回类型为的字符串 系统一串 它由分隔符字符串分隔的值的成员组成。如果值没有成员,则该方法返回 一串空的 .
例外情况: 这种方法可以提供 无理例外 如果T1为空。
例子: 在下面的代码中,首先是一个用户定义的 项目 创建并因此创建了各种项目名称 添加到构造函数中 .此外 列表将保存类“item”类型的对象。 因此,在main方法中,包含类项类型的对象的列表与 分隔符“/” 获取输出字符串值。
// C# program to demonstrate the // Join(String, IEnumerable <T > T1) using System; using System.Collections.Generic; namespace ConsoleApplication2 { // making a user defined data type.. public class items { public string itemname; // constructor to hold the // string values of item class public items( string name1) { itemname = name1; } public override string ToString() { return this .itemname; } } class Geeks { // Main Method static void Main( string [] args) { List<items> alpha = Additems(); // passing the list of objects // of item class to join method( ) string str1 = string .Join( "--" , alpha); Console.WriteLine( "The value of the string is " + str1); } private static List<items> Additems() { // adding the objects of item // class into a list List<items> alpha = new List<items>(); alpha.Add( new items( "fans" )); alpha.Add( new items( "Bulb" )); alpha.Add( new items( "Windows" )); alpha.Add( new items( "table" )); alpha.Add( new items( "chair" )); return alpha; } } } |
The value of the string is fans--Bulb--Windows--table--chair