Concatenates list elements into strings separated by delimiters

Source: Internet
Author: User
Tags string splitter
Code

Public static class EnumerableExtension
{
/// <Summary>
/// Concatenates the list elements into strings separated by splitter
/// </Summary>
/// <Example>
/// Concatenated string:
/// <C> new List & lt; string & gt; {"aa", "bb", "cc "}. montage (p => p, ","); // return value: "aa, bb, cc" </c>
/// Attributes of the spliced object:
/// <C> new List & lt; string & gt; {"aa", "bbb", "c "}. montage (p => p. length. toString (), ","); // return: "2, 3, 1" </c>
/// Spliced enumerated values:
/// <C> new List & lt; DomainType & gt; {DomainType. guanHao, DomainType. yaoJiKe }. montage (p => (int) p ). toString (), ","); // return value: "1, 2" </c>
/// Splicing enumeration Name:
/// <C> new List & lt; DomainType & gt; {DomainType. guanHao, DomainType. yaoJiKe }. montage (p => p. toString (), ","); // return value: "GuanHao, YaoJiKe" </c>
/// </Example>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "source"> </param>
/// <Param name = "toString"> delegate a list element to a string </param>
/// <Param name = "splitter"> separator (can be empty) </param>
/// <Returns> </returns>
Public static string Montage <T> (this IEnumerable <T> source, Func <T, string> toString, string splitter)
{
StringBuilder result = new StringBuilder ();
Splitter = splitter ?? String. Empty;
Foreach (T item in source)
{
Result. Append (toString (item ));
Result. Append (splitter );
}
String resultStr = result. ToString ();
If (resultStr. EndsWith (splitter ))
ResultStr = resultStr. Remove (resultStr. Length-splitter. Length, splitter. Length );
Return resultStr;
}

/// <Summary>
/// Create a generic List from the generic IEnumerable, and each element is converted by converter.
/// </Summary>
/// <Example>
/// Convert the enumerated List to Int32 List:
/// <C> new DomainType [] {DomainType. guanHao, DomainType. yaoJiKe }. toList (p => (int) p); // return: List & lt; int & gt; </c>
/// </Example>
/// <Typeparam name = "TSource"> </typeparam>
/// <Typeparam name = "TResult"> </typeparam>
/// <Param name = "source"> </param>
/// <Param name = "converter"> </param>
/// <Returns> </returns>
Public static List <TResult> ToList <TSource, TResult> (this IEnumerable <TSource> source, Func <TSource, TResult> converter)
{
List <TResult> result = new List <TResult> ();
Foreach (TSource item in source)
{
Result. Add (converter (item ));
}
Return result;
}
}

 

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.