C ++ Join concatenates all elements of the object array

Source: Internet
Author: User
Tags datetime pear

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.