Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am getting the following strings from the database "parent/child", "parent/parent2/parent3/child" , "parent/parent2/parent3/parent4" and so on. The names (parent/child) vary and can be anything.

parent/child
parent/parent2/child
parent/parent2/parent3/child
parent/parent2/parent3/parent4/child

This parent and child are related with a "/". The string comes before "/" is parent and which comes after the "/" is a child. I need to split this string dynamically and enter them into two arrays for parent and child respectively so that I can know who is the father of which child.

So far I have this code, I am stuck with the logic.
C#
foreach(string str in data.strs)
{
if(str.contains("/")
{
string[] names=str.Split('/');
foreach(string name in names)
{
//todo
}
}
}
Posted
Comments
nagendrathecoder 25-Feb-15 22:23pm    
It is not clear. Can you tell how you like to store strings in array after splitting?
JBobby 25-Feb-15 23:04pm    
I wanted to store it like this
parents = all parent entries go here.
childs = all child entries go here.
Which I have bolded in my question. :)
thank you.
Sergey Alexandrovich Kryukov 25-Feb-15 22:40pm    
You know System.String.Split. This is all you need. The rest of it is using your brain. Any problems?
—SA
JBobby 25-Feb-15 23:02pm    
thanks for your suggestion. :)
BillWoodruff 26-Feb-15 1:49am    
I think the solution by Peter Leow may have met your needs, which is great; if you want to know how to parse ALL the lines you show as data, and build a "real" tree structure, please post another question.

1 solution

You are trying to pair up parent and its child, I think using List to contain pairs of key value[^] would be more suitable to capture this relationship. Check out this example:
C#
using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        string str = "parent/parent2/parent3/parent4/child";
        if(str.Contains("/")){
            string[] persons = str.Split('/');

            var kinship = new List<KeyValuePair<string, string>>();

            for(int i=0; i < persons.Length; i++){

                string parent = persons[i];

                string child = i < persons.Length-1? persons[i+1]: "nil";

                kinship.Add(new KeyValuePair<string, string>(parent, child));

                Console.WriteLine("parent: {0}, child: {1}\n", parent, child);
            }
        }
    }
}
 
Share this answer
 
Comments
JBobby 25-Feb-15 22:58pm    
thanks, you answered what I have asked. :)
BillWoodruff 26-Feb-15 0:38am    
+5 a good simplification, for parsing one-line at a time, although it does not actually build a hierarchical structure reflecting all the possible lines of input; i.e., a "real" tree-structure
Peter Leow 26-Feb-15 0:53am    
Thank you, BillWoodruff.

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