Monday, May 26, 2008

C_Sharp edition StringList Class

Delphi有一个常用的类StringList, 它有一个特点是你可以一行一行加String, 如果你所加的每行String都是Name=Value格式的,它会帮你提取出Name列表和Value列表, 这个类我在用Delphi时候, 常使用它. 下面是C#版的StringList类.



namespace LiuHarry.Utils.Foundation
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

public class DelphiStrList
{
private List<string> M_Items = new List<string>();
private List<string> M_Keys = new List<string>();
private string M_KeyValueEqualmark = "=";
private List<string> M_Values = new List<string>();

public virtual void Add(string item)
{
string str;
string str2;
this.M_Items.Add(item);
DivideKeyValue(item, this.M_KeyValueEqualmark, out str2, out str);
this.M_Keys.Add(str2);
this.M_Values.Add(str);
}

public virtual void Clear()
{
this.M_Items.Clear();
this.M_Keys.Clear();
this.M_Values.Clear();
}

protected void DevideKeyValuesByItems()
{
this.M_Keys.Clear();
this.M_Values.Clear();
foreach (string str3 in this.M_Items)
{
string str;
string str2;
DivideKeyValue(str3, this.M_KeyValueEqualmark, out str2, out str);
this.M_Keys.Add(str2);
this.M_Values.Add(str);
}
}

public static void DivideKeyValue(string entireStr, string keyValueEqualmark, out string keyStr, out string valueStr)
{
int index = entireStr.IndexOf(keyValueEqualmark);
if (index > 0)
{
keyStr = entireStr.Substring(0, index);
if (index < (entireStr.Length - 1))
{
valueStr = entireStr.Substring(index + 1);
}
else
{
valueStr = "";
}
}
else
{
keyStr = entireStr;
valueStr = "";
}
}

public virtual int IndexOf(string item)
{
return this.M_Items.IndexOf(item);
}

public virtual void Insert(int index, string item)
{
string str;
string str2;
this.M_Items.Insert(index, item);
DivideKeyValue(item, this.M_KeyValueEqualmark, out str2, out str);
this.M_Keys.Insert(index, str2);
this.M_Values.Insert(index, str);
}

public string MakeItemsToText(string ItemSeperator)
{
return this.MakeStrlistToText(this.M_Items, ItemSeperator);
}

public string MakeKeyItemsToText(string ItemSeperator)
{
return this.MakeStrlistToText(this.M_Keys, ItemSeperator);
}

private string MakeStrlistToText(List<string> items, string ItemSeperator)
{
StringBuilder builder = new StringBuilder();
foreach (string str in items)
{
builder.Append(str);
builder.Append(ItemSeperator);
}
return builder.ToString();
}

public string MakeValueItemsToText(string ItemSeperator)
{
return this.MakeStrlistToText(this.M_Keys, ItemSeperator);
}

public virtual bool Remove(string item)
{
int index = this.IndexOf(item);
if (index >= 0)
{
this.M_Keys.RemoveAt(index);
this.M_Values.RemoveAt(index);
}
return this.M_Items.Remove(item);
}

public virtual void RemoveAt(int index)
{
this.M_Items.RemoveAt(index);
this.M_Keys.RemoveAt(index);
this.M_Values.RemoveAt(index);
}

public virtual void Sort()
{
this.M_Items.Sort();
this.DevideKeyValuesByItems();
}

public int Count
{
get
{
return this.M_Items.Count;
}
}

public List<string> Items
{
get
{
return this.M_Items;
}
}

public List<string> Keys
{
get
{
return this.M_Keys;
}
}

public string KeyValueEqualmark
{
get
{
return this.M_KeyValueEqualmark;
}
set
{
this.M_KeyValueEqualmark = value;
}
}

public List<string> Values
{
get
{
return this.M_Values;
}
}
}
}

No comments: