我會重新考慮你是如何存儲你的數據。我會把路徑數據放入List中。
Employee對象:Employee對象的
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public List<string> PathSuffix { get; set; }
public Employee(int ID, string Name, string PathCSV)
{
EmpId = ID;
EmpName = Name;
PathSuffix = new List<string>();
foreach (string path in PathCSV.Split(','))
{
if (string.IsNullOrEmpty(path) == false && path.Trim().Length > 0)
{
if (PathSuffix.Exists(x => x.ToLower() == path.Trim().ToLower()) == false)
{
PathSuffix.Add(path.Trim());
}
}
}
}
}
樣品
protected void Page_Load(object sender, EventArgs e)
{
List<Employee> MyEmployees = new List<Employee>();
MyEmployees.Add(new Employee(1, "User 1", "Path1,Path1,Path2,Path2,Path2,Path3"));
Response.Write("Employee 1 Information:<br />");
WriteEmployeeToScreen(MyEmployees[0]);
}
protected void WriteEmployeeToScreen(Employee ThisEmployee)
{
System.Text.StringBuilder paths = new System.Text.StringBuilder();
Response.Write(string.Format(
"{1} ({0})<br />",
ThisEmployee.EmpId,
HttpUtility.HtmlEncode(ThisEmployee.EmpName)));
foreach (string path in ThisEmployee.PathSuffix)
{
paths.Append(string.Format(", {0}", path));
}
if (paths.Length > 0)
{
Response.Write(string.Format("Paths: {0}", paths.ToString().Substring(2)));
}
}
OUTPUT:
Employee 1 Information:
User 1 (1)
Paths: Path1, Path2, Path3
請編輯您的問題以包含您的實際類定義(無論它是多長時間)和樣本輸入(作爲可編譯代碼)以及樣本輸出。這使我們很容易給你很好的答案。 – Enigmativity