Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
i have
List<string> list
and it contains value as
XML
[2] "\"Test1\":\"ascdfvgv\"" string
[3] "\"Test2\":\"wqaasss\"" string
[4] "\"Test3\":\"bababalnbala\"" string

how to split this into a simple array of a string without \ and :
Posted
Updated 29-Dec-13 18:38pm
v2

Hi Maulikshah,

You could use the below code to remove \ and : from the list of string
XML
List<string> temp = new List<string>();
            Dictionary<string, string> output = new Dictionary<string, string>();
            temp.Add("\"Test1\":\"ascdfvgv\"");
            temp.Add("\"Test2\":\"wqaasss\"");
            temp.Add("\"Test3\":\"bababalnbala\"");

            for (int i = 0; i < temp.Count; i++)
            {
                temp[i] = temp[i].Replace("\"", "");
                temp[i] = temp[i].Replace(@":", " ");
                temp[j].Split(' ');
                output.Add(temp[j].Split(' ')[0], temp[j].Split(' ')[1]);
            }

            string dicVal = output["Test1"];


Output will be like
Test1 ascdfvgv<br />
Test2 wqaasss<br />
Test3 bababalnbala

But what you mean by split? do you want to split the array in to what?
Update your question with your required output so I could help you out.

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v4
Comments
maulikshah1990 30-Dec-13 1:03am    
hithanks
now if i want this both in array as

array["Test1"]="ascdfvgv";
♥…ЯҠ…♥ 30-Dec-13 1:14am    
Then you could use dictionary, that is better than what you are doing in list.
C#
List<string> lst = new List<string>();
       lst.Add("\"Test1\":\"ascdfvgv\"");
       lst.Add("\"Test2\":\"wqaasss\"");
       lst.Add("\"Test3\":\"bababalnbala\"");

       Dictionary<string, string> dict = new Dictionary<string, string>();
       lst.ForEach(k => { dict.Add(k.Replace("\"", "").Split(':')[0], k.Replace("\"", "").Split(':')[1]); });

       string key = "Test1";

       string value = dict[key];
 
Share this answer
 
v2
Comments
maulikshah1990 30-Dec-13 1:37am    
ok..thanks..but
after select into list as

List<strclass> strValues =
(from x in lst
select new strClass()
{
text = x.Replace("\\", string.Empty).Split(':')[0],
val = x.Replace("\\", string.Empty).Split(':')[1],
}).ToList();

how to search a specific text and according value ,

search

if(key=="Test1")

{

get values of Test1

string valh="ascdfvgv";

}
Karthik_Mahalingam 30-Dec-13 3:47am    
try my latest solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900