You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
using Apewer; using System; using System.Collections.Generic; using System.Text;
namespace Apewer.Models {
/// <summary></summary>
[Serializable] public class StringPairs : List<KeyValuePair<string, string>> {
/// <summary>添加项。返回错误信息。</summary>
public string Add(string key, string value) { if (string.IsNullOrEmpty(key)) return "参数 Name 无效。"; if (string.IsNullOrEmpty(value)) return "参数 Value 无效。";
var kvp = new KeyValuePair<string, string>(key, value); Add(kvp); return null; }
/// <summary>获取所有 Key。</summary>
public string[] GetAllKeys() { var keys = new string[Count]; for (var i = 0; i < Count; i++) keys[i] = this[i].Key; return keys; }
/// <summary>获取匹配 Key 的所有 Value,不存在 Key 或 Value 时返回空数组。</summary>
public string[] GetValues(string key, bool ignoreCase = true, bool withEmpty = true) { if (string.IsNullOrEmpty(key)) return new string[0];
var lowerArg = TextUtility.ToLower(key); var values = new List<string>(); foreach (var item in this) { if (item.Key == key) { if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value); continue; }
if (ignoreCase) { var lowerItem = TextUtility.ToLower(item.Key); if (lowerItem == lowerArg) { if (withEmpty || !string.IsNullOrEmpty(item.Value)) values.Add(item.Value); continue; } } } return values.ToArray(); }
/// <summary>获取匹配 Key 的 Value。不存在 Key 时返回 NULL 值。</summary>
public string GetValue(string key, bool ignoreCase = true) { if (string.IsNullOrEmpty(key)) return null;
var lowerArg = TextUtility.ToLower(key); var matched = false; foreach (var item in this) { if (item.Key == key) return item.Value;
if (ignoreCase) { var lowerItem = TextUtility.ToLower(item.Key); if (lowerItem == lowerArg) { matched = true; if (!string.IsNullOrEmpty(item.Value)) return item.Value; } } }
return matched ? "" : null; }
/// <summary>对 Key 升序排序。</summary>
public void Sort() { SortAsc(); }
/// <summary>对 Key 升序排序。</summary>
public void SortAsc() { base.Sort(new Comparison<KeyValuePair<string, string>>((a, b) => a.Key.CompareTo(b.Key))); }
/// <summary>对 Key 降序排序。</summary>
public void SortDesc() { base.Sort(new Comparison<KeyValuePair<string, string>>((b, a) => a.Key.CompareTo(b.Key))); }
}
}
|