Convert string to Hash Table

Rajni.Shekhar
Posted by Rajni.Shekhar under C# category on | Points: 40 | Views : 6852
public static Hashtable Convert_StrToArry(string str, char chDel1, char chDel2) //string with delimator ex: str="C1~C2;A~B" chDel1='~' and chDel2=';'
{
Hashtable hashTable = null;
object[] obj1 = null;
object[] obj2 = null;
try
{
if (str!= string.Empty)
{
hashTable = new Hashtable();
if (str.Contains(chDel2))
{
object[] StrSplit = str.Split(chDel2);

if (StrSplit[1].ToString().Contains(chDel1))
{
obj2 = StrSplit[1].ToString().Split(chDel1);
}
else
{
obj2 = new object[1];
obj2[0] = StrSplit[1].ToString();
}
if (StrSplit[0].ToString().Contains("~"))
{
obj1 = StrSplit[0].ToString().Split('~');
}
else
{
obj1 = new object[1];
obj1[0] = StrSplit[0].ToString();
}

if (obj1.Length > 0 && obj2.Length > 0)
{
for (int j = 0; j < obj1.Length; j++)
{
hashTable.Add(obj1[j], obj2[j]);
}
}
}
}
}
catch (Exception ex)
{

}
return hashTable;
}

Comments or Responses

Login to post response