Skip to content

How to split the line by line

Sin Yee

Asked Apr 1, 2011, 2:06 PM EDT1 answer
Share
LinkedInX
hi..i am doing split now. But i face some problem.

For example'
i got data in String array
"1 2"
"1 2 3"
"2 3"
.......
But, after i split the data
All will become like this
1
2
1
2
3
2
3
but this is not i want..
the expected output that i want is like this
Output 1 
1
2
output2
1
2
3
output3
2
3
and so on......this is my coding. hope someone can help me to solve this problem
C#
   List<KeyValuePair<String, int>> P = new List<KeyValuePair<String, int>>(itemS.ToList());
                        KeyitemS.Clear();


                        String[] SplitA;
                        String[] SplitB;
                        List<String> l = new List<String>();
                        foreach (KeyValuePair<String, int> pair in P)
                        {
                            KeyitemS.AppendText(pair.Key + ":" + pair.Value + "\r\n");
                            l.Add(pair.Key);
                        }

                        String ink ="";
                        List <String> U =new List<String>();
                        foreach (String inter in l)
                        {

                           // MessageBox.Show(inter);
                            SplitA = inter.Split(' ');
                            SplitB = new String[SplitA.Length];
                            for (int i = 0; i < SplitA.Length; i++)
                            {
                                SplitB[i] = SplitA[i];
                                U.Add(SplitB[i]);
                            }
                            HashSet<string> hset1 = new HashSet<string>(v);
                            HashSet<string> hset2 = new HashSet<string>(U);
                            hset1.IntersectWith(hset2);

                            foreach (string inter1 in hset1)
                            {
                                ink += inter1;

                            }
                            MessageBox.Show(ink);

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Suthish Nair
    0
    I will not say this a good logic, just for understanding or wait for more replies..

                string[] s1 = { "1 2" };
                string[] s2 = { "1 2 3" };
                string[] s3 = { "2 3" };
                List<string[]> s = new List<string[]>();
                s.Add(s1);
                s.Add(s2);
                s.Add(s3);

                for (int i = 0; i < s.Count; i++)
                {
                    Console.WriteLine("Output " + (i + 1));
                    foreach (string item in s[i][0].ToString().Split(' '))
                    {
                        Console.WriteLine(item.Trim());
                    } 
                }
                Console.ReadLine();
    0