程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Dictionary<string, string> 排序,dictionarystring

Dictionary<string, string> 排序,dictionarystring

編輯:C#入門知識

Dictionary<string, string> 排序,dictionarystring


.net framework 2.0 版

Dictionary<string, string> collection = new Dictionary<string, string> ();

collection.Add("key3","value3");

collection.Add("key1","value1");

collection.Add("key4","value4");

collection.Add("key2","value2");

List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(collection);

list.Sort(delegate(KeyValuePair<string, string> pair1, KeyValuePair<string, string> pair2)
{
  return pair1.Key.CompareTo(pair2.Key);
});

StringBuilder sb = new StringBuilder();

foreach (KeyValuePair<string, string> pair in list)
{
  sb.Append(pair.Key);
  sb.Append("=");
  sb.Append(pair.Value);
  sb.Append("&");
}

string str = sb.ToString().TrimEnd('&');

//str 值key1=value1&key2=value2&key3=value3&key4=value4

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved