Join ()-concatenates each element of the object array, where the specified separator is used between each element
Parameters
Separator
Type: System. String
The string to be used as the separator.
Values
Type: System. Object []
An array containing the elements to be connected.
Return Value
Type: System. String
A string composed of values elements, which are separated by the separator string.
Take the following example and use StringBuilder to concatenate a set of strings:
String [] parts = {"Apple", "Orange", "Banana", "Pear", "Peach "};
Var builder = new StringBuilder ();
For (int I = 0; I <parts. Length; I ++)
{
Builder. Append (parts [I]);
// Remove the last ','
If (I! = Parts. Length-1)
{
Builder. Append (",");
}
}
// The result is "Apple, Orange, Banana, Pear, Peach"
Var result = builder. ToString ();
String. Join () can be easily implemented:
String [] parts = {"Apple", "Orange", "Banana", "Pear", "Peach "};
Var result = string. Join (",", parts );
Many people do not realize that Join () can merge any types, such as int, DateTime, double, or other custom types!
When string. when Join () merges non-string values, it actually causes each element ToString (). that is to say, the ToString () definition of these elements meets your needs-although most asp tutorials. the types in the. net library already exist.
For example:
// Merge integer "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
Var numsFromOneToTen = string. Join (",", Enumerable. Range (1, 10 ));
// Merge values of different types => "1-3.1415927-9/16/2011 12:52:22"
Var variousObjects = string. Join ("-", new object [] {1, 3.1415927, DateTime. Now });
Finally, Join () obviously supports IEnumerable <T> and object [], string []:
String [] arr = {"one", "two", "three "};
Console. WriteLine (string. Join (",", arr ));
// In. net 4.0, you can list all values directly without generating arrays.
Var numsFromOneToTen = string. Join (",", "A", "B", "C", "D", "E ");
Var variousObjects = string. Join ("-", 1, 3.1415927, DateTime. Now );