/// <summary> /// Format a List Of T /// </summary> /// <typeparam name="T"></typeparam> /// <param name="l"></param> /// <param name="format"></param> /// <param name="separator"></param> /// <param name="preFix"></param> /// <param name="postFix"></param> /// <returns></returns> public static string Format <T>(List <T> l, string format, string separator = ", ", string preFix = "", string postFix = "") { System.Text.StringBuilder b = new StringBuilder(1024); foreach (var e in l) { b.AppendFormat(format, ExtendedFormat.FormatValue(e)); b.Append(separator); } string r = b.ToString(); if (r.EndsWith(separator)) { r = r.Substring(0, r.Length - separator.Length); } return(preFix + r + postFix); }
/// <summary> /// Format the content of a dictionary to a string /// </summary> /// <typeparam name="K">The type of the key</typeparam> /// <typeparam name="V">The type of the value</typeparam> /// <param name="dictionary">The dictionary to FormatString</param> /// <param name="format">The FormatString of each key and value. The default is "{0}:{1}"</param> /// <param name="separator">The separator between each key and value. The default is ", "</param> /// <param name="preFix">A string to output at the beginning of the string, the default is "{ "</param> /// <param name="postFix">A string to output at the end of the string, the default is " }"</param> /// <returns></returns> public static string Format <K, V>(IDictionary <K, V> dictionary, string format = "{0}:{1}", string separator = ", ", string preFix = "{ ", string postFix = " }") { if (dictionary == null) { return("Null"); } // just in case System.Text.StringBuilder b = new StringBuilder(1024); b.Append(preFix); foreach (KeyValuePair <K, V> kv in dictionary) { b.AppendFormat(format, ExtendedFormat.FormatValue(kv.Key, false), ExtendedFormat.FormatValue(kv.Value, true)); b.Append(separator); } b.Remove(b.Length - separator.Length, separator.Length); // Remove the separator b.Append(postFix); return(b.ToString()); }
/// <summary> /// Format a IDictionary of K,V to a { key1:value1, key2:value2} string. /// Type string and datetime are doublequoted for the keys and values. /// </summary> /// <typeparam name="K"></typeparam> /// <typeparam name="V"></typeparam> /// <param name="dict"></param> /// <returns></returns> public static string DictionaryToString <K, V>(IDictionary <K, V> dict) { if (dict == null) { return("Null"); } // just in case System.Text.StringBuilder b = new StringBuilder(1024); b.Append("{"); foreach (KeyValuePair <K, V> kv in dict) { string value = ExtendedFormat.FormatValue(kv.Value); string key = ExtendedFormat.FormatValue(kv.Key); b.AppendFormat("{0}:{1}, ", key, value); } b.Remove(b.Length - System.Environment.NewLine.Length, System.Environment.NewLine.Length); // Remove the coma and the space b.Append("}"); return(b.ToString()); }