Why would you want it in that format:
D1//D4//D9//S001//S002//S003
How can you tell the difference between:
D1
1____D4
1 1_____D9
1 1 1_____S001
1 1 1_____S002
1 1 1_____S003
and
D1
1____D4
1 1_____D9
1 1 1_____S001
1 1 1 1_____S002
1 1 1 1_____S003
(Sorry to make it an answer, but you can't put pictures well in comments).
"thanks for ur reply.
in my in button click event i have to sent the above output.
in my tree view D9 have 3 childes. that childes does not have any childes.
i want last node Path which is having childes and i have to concatenate that node childes which is doesn't have any childes.
I have to send to an embedded device. they required like that bcz they have less memory. plz help me"
That's a little complex (and so is your solution, but Hey! It works!
As a result, I'm almost afraid to touch your version, and would add an extra coding step as a post-process to compact your data.
Here is my version (I use Lists instead of ArrayList because they are type safe):
List<string> uncompacted = new List<string>();
uncompacted.Add("D1//D4//D9//S001");
uncompacted.Add("D1//D4//D9//S002");
uncompacted.Add("D1//D4//D9//S003");
uncompacted.Add("D1//D4//D10//S004");
uncompacted.Add("D1//D4//D10//S005");
uncompacted.Add("D1//D4//D11//S006");
uncompacted.Add("D2//D5//D12//S007");
uncompacted.Add("D2//D5//S008");
uncompacted.Add("D2//D6//S009");
uncompacted.Add("D2//D6//S010");
uncompacted.Add("D3//D16");
uncompacted.Add("D3//S011");
List<string> compacted = new List<string>();
string basis = "****";
StringBuilder sb = new StringBuilder();
foreach (string s in uncompacted)
{
if (s.StartsWith(basis))
{
sb.Append(s.Substring(basis.Length));
}
else
{
if (sb.Length != 0)
{
compacted.Add(sb.ToString());
}
basis = s.Substring(0, s.LastIndexOf('/'));
sb = new StringBuilder(s);
}
}
if (sb.Length != 0)
{
compacted.Add(sb.ToString());
}
foreach (string s in compacted)
{
Console.WriteLine(s);
}
"S001
S002
S003 are sensors. that sensors doesnot have any childes. so i need like that and also evry Di node must have one or more sendores."
Yes... and the output of my code is:
D1//D4//D9//S001/S002/S003
D1//D4//D10//S004/S005
D1//D4//D11//S006
D2//D5//D12//S007
D2//D5//S008
D2//D6//S009/S010
D3//D16/S011
Which is what you asked for...
"Thanks for ur replay but its not give derided out put."
Oh come on! That's not exactly difficult to fix, is it! Change one line, add just "- 1":
basis = s.Substring(0, s.LastIndexOf('/') - 1);
"its working...
i have to inherit into my code. thanks allot.
May i know what is type safe.
Why u using list instead of array list."
When you use an ArrayList, you can add any items to it:
ArrayList ar = new ArrayList();
ar.Add("hello");
ar.Add(new Point(1, 2));
ar.Add(new formMain());
That means when you access the items later, you have to check what type they are and cast them appropriately. If your code does not check for a particular type then it will either be unable to handle it, or will fail at run time.
If you use a List<T>, then you can only store one type (or derivatives) in the list. As a result, you don't have to cast, or check, because the compiler does it for you. If you try to a add different type to the list, the compiler complains and your mistake is caught earlier.
List<string> uncompacted = new List<string>();
Declares a list of strings, and any attempt to add an int or Point will be complained about by the compiler. We can then use the content much more readably:
foreach (string s in uncompacted)
{
...
}
That's what we mean by "type safe": you can't add the wrong type to a type safe list.