C # How can I add the previous element to an array to the next element of an array for an unknown number of elements?
How can I add the previous element in an array to the next element in an array for an unknown number of elements?
Here is what I am trying to do. I have a line containing an LDAP path such as "OU = 3, OU = 2, OU = 1, DC = Internal, DC = Net", I want to create each container in the above LDAP path, so from the above line I need to create an array with below content so I can create each container. The first element of the array needs to be created before I can create the second, etc.
"OU = 1, DC = Internal, DC = Net"
"OU = 2, OU = 1, DC = Internal, DC = Net"
"OU = 3, OU = 2, OU = 1, DC = Internal, DC = Net"
My example string is just an example, so the path can be longer or shorter and can contain one array element or 10+, I just don't know, so I won't know how many array elements there are, I need to go through all of them, so I have all the paths in an array.
Another example:
From "OU = Test4, OU = Number3, OU = Item2, OU = 1, DC = Internal, DC = Net" I need:
"OU = 1, DC = Internal, DC = Net" "OU = Item2, OU = 1, DC = Internal, DC = Net" "OU = number3, OU = Item2, OU = 1, DC = Internal, DC = Net "" OU = Test4, OU = number3, OU = Element2, OU = 1, DC = Internal, DC = Net "
Thanks for the help with this.
J
I'm not sure I understood correctly. You need something like this:
string input = "OU=3,OU=2,OU=1,DC=Internal,DC=Net";
string[] split = input.Split(',');
string path = "";
for (int i=split.Length-1; i>=0; i--)
{
path = ((path == "") ? split[i] : split[i] + "," + path);
if (path.StartsWith("OU"))
DoSomething(path);
}
This will call DoSomething () three times, for the following lines:
- OU = 1, DC = Internal, DC = Net
- OU = 2, OU = 1, DC = Internal, DC = Net
- OU = 3, OU = 2, OU = 1, DC = Internal, DC = Net
source to share
If the line always ends with ", DC = Internal, DC = Net", the next solution should catch you.
static string[] Split(string path)
{
const string postfix = ",DC=Internal,DC=Net";
string shortPath = path.Substring(0, path.Length - postfix.Length);
return shortPath.Split(',').Select(x => x + postfix).ToArray();
}
Edit solution 2.0
static string[] Split(string path)
{
const string postfix = ",DC=Internal,DC=Net";
string shortPath = path.Substring(0, path.Length - postfix.Length);
string[] items = shortPath.Split(',');
List<string> final = new List<string>();
foreach ( string item in items ) {
final.Add(item + postfix);
}
return final.ToArray();
}
source to share
Here's some sample code (assuming you have .NET 3.5):
// First we need to split up the initial string
string[] items = initialString.Split("'");
// Then we filter it so that we have a list of OU and non-OU items
string[] ouItems = items.Where(s=>s.StartsWith("OU=")).ToArray();
string[] nonOuItems = items.Where(s=>!s.StartsWith("OU=")).ToArray();
List<string> mainList = new List<string>();
// Our main loop
for (int i = 0; i < ouItems.Length; i++)
{
List<string> localList = new List<string>();
// We loop through all the previous items including the current one
for (int j = 0; j <= i; j++)
{
localList.Add(ouItems[i]);
}
// Then we add all of the non-OU items
localList.AddRange(nonOuItems);
// Then we build the string itself
bool first = true;
StringBuilder sb = new StringBuilder();
foreach (string s in localList)
{
if (first) first = false;
else sb.Append (","); // Separating the items with commas
sb.Append(s);
}
mainList.Add(sb.ToString());
}
// Now we only have to convert it back to an array
string[] finalArray = mainList.ToArray();
Even if you don't have .NET 3.5, it's fairly easy to port it back to .NET 2.0. Also, I have not tested this, so there may be errors or typos. Also, this is definitely not the most efficient way to do it, but I think it is quite obvious and will probably work.
source to share
This should give you the array in the correct order, a little longer since this is a real console application:
using System;
using System.Text;
namespace Splitomatic
{
public class Program
{
/// <summary>
/// Sample Input:
/// arg[0]: OU=Test4,OU=Number3,OU=Item2,OU=1
/// arg[1]: DC=Internal,DC=Net
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
StringBuilder builder;
string[] containers;
string[] parts;
if (args.Length != 2)
{
Console.WriteLine("Usage: <exe> <containers> <append");
}
builder = new StringBuilder(args[1]);
parts = args[0].Split(',');
containers = new string[parts.Length];
for (int i = parts.Length - 1; i >= 0; --i)
{
builder.Insert(0, ",");
builder.Insert(0, parts[i]);
containers[Math.Abs(i - parts.Length + 1)] = builder.ToString();
}
Console.WriteLine("Dumping containers[]:");
for (int i = 0; i < containers.Length; ++i)
{
Console.WriteLine("containers[{0}]: {1}", i, containers[i]);
}
Console.WriteLine("Press enter to quit");
Console.ReadLine();
}
}
}
Output example:
Dumping containers []: containers [0]: OU = 1, DC = Internal, DC = Net containers [1]: OU = Item2, OU = 1, DC = Internal, DC = Net containers [2]: OU = Number3, OU = Item2, OU = 1, DC = Internal, DC = Net containers [3]: OU = Test4, OU = Number3, OU = Item2, OU = 1, DC = Internal, DC = Net Press enter to quit
source to share
One method. This will also create a row for each of the domain controllers.
string[] GetArray(string input)
{
string[] vals = input.Split(',');
List<string> entries = new List<string>();
string s;
for(int i=vals.Length - 1; i > 0; i--)
{
s = vals[i];
if(i < vals.Length - 1)
s += "," + entries[(vals.Length - 2) - i];
entries.Add(s);
}
return entries.ToArray();
}
source to share
My code is almost like Martin, but his code prints a comma to the end of the entire path object.
Here's the correct function:
static string[] SplitLDAPPath(string input)
{
List<String> r = new List<string>();
string[] split = input.Split(',');
string path = "";
for (int i = split.Length - 1; i >= 0; i--)
{
path = split[i] + "," + path;
if (path.StartsWith("OU"))
r.Add(path.Substring(0, path.Length-1));
}
return r.ToArray();
}
through:
String s = "OU=Test4,OU=Number3,OU=Item2,OU=1,DC=Internal,DC=Net";
String[] r = SplitLDAPPath(s);
foreach (String ss in r)
lt.Text += ss + "<br/>";
outputs:
OU=1,DC=Internal,DC=Net
OU=Item2,OU=1,DC=Internal,DC=Net
OU=Number3,OU=Item2,OU=1,DC=Internal,DC=Net
OU=Test4,OU=Number3,OU=Item2,OU=1,DC=Internal,DC=Net
source to share
If I understand your question correctly:
- Split string by semicolon, producing N elements
- The method should result in multiple lines
- The first line must be the last item after the semicolon split
- The next item must be what came before the last item, semicolon and last item
- The next element must match all previous elements, a semicolon and then the previous element
Then the implementation should be pretty simple:
public static IEnumerable<String> LastToFirstOrSomething(String s)
{
String[] parts = s.Split(';');
String result = String.Empty;
for (Int32 index = parts.Length - 1; index >= 0; index--)
{
if (result.Length > 0)
result = ";" + result;
result = parts[index] + result;
yield return result;
}
}
Of course this could be improved, from a performance standpoint, using StringBuilder.
Now having said all this, I know your question is not 100% true to my answer because you didn't say why you don't provide the first X elements, you are missing them. Why is this?
You need to provide more information for a better answer.
source to share