Joins the key/value pairs into a string
using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Xml;
namespace System.Collections.Specialized
{
/// <summary>
/// Represents a collection of useful extenions methods.
/// </summary>
public static class NameValueCollectionExtensons
{
/// <summary>
/// Joins the key/value pairs into a string of the follwing format:
/// "Key1=Value1|Key2=Value2|...".
/// </summary>
public static string Join(this NameValueCollection collection)
{
return Join(collection, "|");
}
/// <summary>
/// Joins the key/value pairs into a string of the follwing format:
/// "Key1=Value1|Key2=Value2|...".
/// </summary>
public static string Join(this NameValueCollection collection, string separator)
{
if(string.IsNullOrEmpty(separator))
throw new ArgumentNullException("separator");
StringBuilder sb = new StringBuilder();
foreach(string key in collection.AllKeys)
sb.AppendFormat("{0}={1}{2}", key, (collection[key] ?? string.Empty).ToString(), separator);
return sb.ToString().TrimEnd(separator.ToCharArray());
}
}
}
Related examples in the same category