<code>using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public static class JoinStrExtensions
{
/// <summary>
/// String join, with separator.
/// </summary>
/// <param name="separator">Separator string</param>
/// <returns>The joined string.</returns>
/// <remarks>
/// </remarks>
public static string JoinStr(this IEnumerable<string> l, string separator)
{
return string.Join(separator, l);
}
/// <summary>
/// String join, with separator and final separator.
/// </summary>
/// <param name="separator">Regular separator, separates words except the two last ones.</param>
/// <param name="finalSeparator">Final separator, separates the two last ones.</param>
/// <param name="strs"></param>
/// <returns></returns>
/// <remarks>Ignores null and empty strings.</remarks>
public static string JoinStr(this IEnumerable<string> strs, string separator, string finalSeparator)
{
var sb = new StringBuilder();
int i = strs.Count();
var nonEmpty = strs.Where(s => !string.IsNullOrEmpty(s));
if (nonEmpty.Count >= 1)
{
sb.Append(nonEmpty(0));
}
for (i = 1; i <= nonEmpty.Count - 2; i++)
{
sb.Append(separator + nonEmpty(i));
}
if (nonEmpty.Count >= 2)
{
sb.Append(finalSeparator + nonEmpty(i));
}
return sb.ToString();
}
/// <summary>
/// Extended join, with separator and global prefix/suffix.
/// </summary>
/// <param name="globalprefix">Global prefix</param>
/// <param name="separator">Separator string</param>
/// <param name="globalsuffix">Global suffix</param>
/// <returns>The joined string.</returns>
/// <remarks>
/// <para>This method joins the collection with the separator and
/// (if the original array is not empty), the global prefix and suffix
/// are added.</para>
/// </remarks>
/// <example>
/// The following call will render a string in vector form, with
/// parentheses and comma-separated, BUT will return the empty string
/// if the collection has zero elements:
/// <code>
/// List<string> myCollection;
/// string htmlList;
/// ...
/// htmlList = myCollection.JoinStr("(", ", ", ")");
/// </code>
/// </example>
public static string JoinStr(this IEnumerable<string> l, string globalprefix, string separator, string globalsuffix)
{
if (l.Count() == 0)
{
return "";
}
else
{
return globalprefix + l.JoinStr(separator) + globalsuffix;
}
}
/// <summary>
/// Extended join, with item prefixes/suffixes, and global prefix/suffix.
/// </summary>
/// <param name="globalprefix">Global prefix</param>
/// <param name="itemprefix">Item prefix</param>
/// <param name="itemsuffix">Item suffix</param>
/// <param name="globalsuffix">Global suffix</param>
/// <returns>The joined string.</returns>
/// <remarks>
/// <para>This method is really a combination of a Map operation and a Join.</para>
/// <para>The Map operation substitutes each element <c>x</c> with:
/// <code>
/// itemprefix + x + itemsuffix
/// </code>
/// </para>
/// <para>Then, the resulting array is joined with the empty separator and
/// finally (if the original array is not empty), the global prefix and suffix
/// are added.</para>
/// </remarks>
/// <example>
/// The following call will convert a string collection into an HTML list,
/// but will return the empty string if the collection has zero elements:
/// <code>
/// List<string> myCollection;
/// string htmlList;
/// ...
/// htmlList = myCollection.JoinStr("&lt;ul>", "&lt;li>", "&lt;/li>", "&lt;/ul>");
/// </code>
/// The following example generates a pretty printed text list:
/// <code>
/// List<string> myCollection;
/// string textList;
/// ...
/// textList = myCollection.JoinStr("My list:" &amp; vbCrLf, " - ", vbCrLf, "");
/// </code>
/// </example>
public static string JoinStr(this IEnumerable<string> l, string globalprefix, string itemprefix, string itemsuffix, string globalsuffix)
{
if (l.Count() == 0)
{
return "";
}
else
{
return globalprefix + itemprefix + l.JoinStr(itemsuffix + itemprefix) + itemsuffix + globalsuffix;
}
}
/// <summary>
/// Extended join, with item prefixes/suffixes, separators and global prefix/suffix.
/// </summary>
/// <param name="globalprefix">Global prefix</param>
/// <param name="itemprefix">Item prefix</param>
/// <param name="separator">Separator</param>
/// <param name="itemsuffix">Item suffix</param>
/// <param name="globalsuffix">Global suffix</param>
/// <returns>The joined string.</returns>
/// <remarks>
/// <para>This method is really a combination of a Map operation and a Join.</para>
/// <para>The map operation substitutes each element <c>x</c> with:
/// <code>
/// itemprefix &amp; x &amp; itemsuffix
/// </code>
/// </para>
/// <para>Then, the resulting array is joined with the given separator and
/// finally (if the original array is not empty), the global prefix and suffix
/// are added.</para>
/// </remarks>
/// <example>
/// The following call will convert a string collection into an HTML list,
/// pretty printed,
/// but will return the empty string if the collection has zero elements:
/// <code>
/// List<string> myCollection;
/// string htmlList;
/// ...
/// htmlList = myCollection.JoinStr("&lt;ul>", "&lt;li>", vbCrLf, "&lt;/li>", "&lt;/ul>");
/// </code>
/// The following example generates a parenthesized, comma-sepparated, quoted string list:
/// <code>
/// List<string> myCollection;
/// string textList;
/// ...
/// textList = myCollection.JoinStr("(", "'", ", ", "'", ")");
/// </code>
/// It would return, for example, <c>('str1', 'str2', 'str3')</c>.
/// </example>
public static string JoinStr(this IEnumerable<string> l, string globalprefix, string itemprefix, string separator, string itemsuffix, string globalsuffix)
{
if (l.Count() == 0)
{
return "";
}
else
{
return globalprefix + itemprefix + l.JoinStr(itemsuffix + separator + itemprefix) + itemsuffix + globalsuffix;
}
}
/// <summary>
/// String join extension function for lists of strings. It also
/// accepts a mapping function for the strings.
/// </summary>
/// <param name="l"></param>
/// <param name="sep"></param>
/// <param name="conv"></param>
/// <returns></returns>
/// <remarks></remarks>
public static string JoinStr<T>(this IEnumerable<T> l, string sep, Func<T, string> conv = null)
{
StringBuilder sb = new StringBuilder();
int i = 0;
bool firstOne = true;
if (conv == null)
{
conv = obj => obj.ToString();
}
do
{
if (i > l.Count() - 1)
{
return sb.ToString();
}
else if (string.IsNullOrEmpty(conv(l.ElementAt(i))))
{
i += 1;
}
else
{
if (firstOne)
{
sb.Append(conv(l.ElementAt(i)));
firstOne = false;
}
else
{
sb.Append(sep + conv(l.ElementAt(i)));
}
i += 1;
}
} while (true);
}
}
</code>