What's the most efficient way to convert a Dictionary to a formatted string.
e.g.:
My method:
public string DictToString(Dictionary<string, string> items, string format){
format = String.IsNullOrEmpty(format) ? "{0}='{1}' " : format;
string itemString = "";
foreach(var item in items){
itemString = itemString + String.Format(format,item.Key,item.Value);
}
return itemString;
}
Is there a better/more concise/more efficient way?
Note: the Dictionary will have at most 10 items and I'm not committed to using it if another similar "key-value pair" object type exists
Also, since I'm returning strings anyhow, what would a generic version look like?
I just rewrote your version to be a bit more generic and use StringBuilder
:
public string DictToString<T, V>(IEnumerable<KeyValuePair<T, V>> items, string format)
{
format = String.IsNullOrEmpty(format) ? "{0}='{1}' " : format;
StringBuilder itemString = new StringBuilder();
foreach(var item in items)
itemString.AppendFormat(format, item.Key, item.Value);
return itemString.ToString();
}
public string DictToString<TKey, TValue>(Dictionary<TKey, TValue> items, string format)
{
format = String.IsNullOrEmpty(format) ? "{0}='{1}' " : format;
return items.Aggregate(new StringBuilder(), (sb, kvp) => sb.AppendFormat(format, kvp.Key, kvp.Value)).ToString();
}
This method
public static string ToFormattedString<TKey, TValue>(this IDictionary<TKey, TValue> dic, string format, string separator)
{
return String.Join(
!String.IsNullOrEmpty(separator) ? separator : " ",
dic.Select(p => String.Format(
!String.IsNullOrEmpty(format) ? format : "{0}='{1}'",
p.Key, p.Value)));
}
used next way:
dic.ToFormattedString(null, null); // default format and separator
will convert
new Dictionary<string, string>
{
{ "a", "1" },
{ "b", "2" }
};
to
a='1' b='2'
or
dic.ToFormattedString("{0}={1}", ", ")
to
a=1, b=2
Don't forget an overload:
public static string ToFormattedString<TKey, TValue>(this IDictionary<TKey, TValue> dic)
{
return dic.ToFormattedString(null, null);
}
You can use generic TKey
/TValue
because any object has ToString()
which will be used by String.Format()
.
And as far as IDictionary<TKey, TValue>
is IEnumerable<KeyValuePair<TKey, TValue>>
you can use any. I prefer IDictionary for more code expressiveness.
Format dictionary in one line with Linq and string.Join() (C# 6.0):
Dictionary<string, string> dictionary = new Dictionary<string, string>()
{
["key1"] = "value1",
["key2"] = "value2"
};
string formatted = string.Join(", ", dictionary.Select(kv => $"{kv.Key}={kv.Value}")); // key1=value1, key2=value2
You can create simple extension method like this:
public static class DictionaryExtensions
{
public static string ToFormatString<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, string format = null)
{
format = string.IsNullOrEmpty(format) ? "{0}='{1}'" : format;
return string.Join(", ", dictionary.Select(kv => string.Format(format, kv.Key, kv.Value)));
}
}
Sligtly improved version of the other answers, using extension methods and default parameters, and also wrapping key/value pairs in {}:
public static string ItemsToString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> items, string format = "{0}='{1}' ")
{
return items
.Aggregate(new StringBuilder("{"), (sb, kvp) => sb.AppendFormat(format, kvp.Key, kvp.Value))
.Append('}')
.ToString();
}
The method can then be called directly from the dictionary/enumerable:
string s = myDict.ItemsToString()
I think efficiency is hardly a concern with only 10 strings, but maybe you don't want to rely on it only being ten.
Concatenation of Strings creates a new String object in memory, since String objects are immutable. This also suggest other String operations may create new instances, like replace. Usually this is avoided by using StringBuilder.
StringBuilder avoids this by using a buffer which it operates on; when the value of the StringBuilder is concatenated with another String the contents are added to the end of the buffer.
However there are caveats, see this paragraph:
Performance considerations
[...]
The performance of a concatenation operation for a String or StringBuilder object depends on how often a memory allocation occurs. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation only allocates memory if the StringBuilder object buffer is too small to accommodate the new data. Consequently, the String class is preferable for a concatenation operation if a fixed number of String objects are concatenated. In that case, the individual concatenation operations might even be combined into a single operation by the compiler. A StringBuilder object is preferable for a concatenation operation if an arbitrary number of strings are concatenated; for example, if a loop concatenates a random number of strings of user input.
So a (contrived) case like this should probably not be replaced with StringBuilder:
string addressLine0 = Person.Street.Name + " " + Person.Street.Number + " Floor " + Person.Street.Floor;
...as the compiler might be able to reduce this to a more efficient form. It is also highly debatable if it would inefficient enough to matter in the greater scheme of things.
Following Microsoft's recommendations you probably want to use StringBuilder instead (like the other highly adequate answers show.)
Gabe, if you are going to be generic, be generic:
public string DictToString<T>(IDictionary<string, T> items, string format)
{
format = String.IsNullOrEmpty(format) ? "{0}='{1}' " : format;
StringBuilder itemString = new StringBuilder();
foreach(var item in items)
itemString.AppendFormat(format, item.Key, item.Value);
return itemString.ToString();
}
来源:https://stackoverflow.com/questions/3639094/most-efficient-dictionaryk-v-tostring-with-formatting