3

I am trying to convert the following string:

"CN=Test,OU=ABC,OU=Company,DC=CFLA,DC=domain"

into this form (a list of string arrays):

[
  {"CN" , "Test"},
  {"OU" , "ABC"},
  {"OU" , "Company"},
  {"DC" , "CFLA"},
  {"DC" , "domain"},
]

What I have tried already is converting the string into a list of dictionaries. I tried splitting the string twice. First on the basis of , and then on the basis of =.

Following is the code I used:

var result = str.Split(',')
                .Select(line => line.Split('='))
                .ToDictionary(b=> b[0], b=> b[1])
                .ToList();

But it gives me the following exception: 'System.ArgumentException: 'An item with the same key has already been added', rightly so as 'OU' and 'DC' are being repeated as keys.

What I want now is to split/convert/process this string into a List of string arrays (as shown above). Is it possible?

Or kindly guide me with an alternate solution. Thanks in advance.

P.S. I have hundreds of these strings and I only want the value of the first "OU" from every string.

1
  • 2
    Just remove your ToDictionary call, and you'll already have a list of string arrays. Although if the goal is just "to get the first OU from each string" then there are simpler approaches. Commented Oct 15, 2019 at 11:12

3 Answers 3

8

If you really want a List of string-Arrays, just omit the "ToDictionary" line.

var result = str.Split(',')
            .Select(line => line.Split('='))
            .ToList();

will give you a List of Arrays, with each Array [0] holding the "Key", and [1] holding the "Value"

ad PS: To get the first "OU" value, you can use a regular expression:

var firstOuRegex = new Regex("[oO][uU]=([^,]+)", RegexOptions.Compiled);
var testString = "CN=Test,OU=ABC,OU=Company,DC=CFLA,DC=domain";

var result = firstOuRegex.Match(testString).Groups[1];
Sign up to request clarification or add additional context in comments.

1 Comment

Probably better to just use regex for the entire solution if you're going to use it at all.
2

You have two "OU" entries in your expression. As a result, you cannot translate it to a dictionary. Instead, translate it to a list of KeyValuePair

var result = str.Split(',')
            .Select(line => line.Split('='))
            .Select(tuple => new KeyValuePair<string, string>(tuple[0], tuple[1]))
            .ToList();

Comments

1

I suppose the code below will provide you with the required result:

class Program
{
    static void Main(string[] args)
    {
        string initialString = "CN=Test,OU=ABC,OU=Company,DC=CFLA,DC=domain";

        List<string[]> finalList = new List<string[]>();
        string [] firstSplitStringsArray = initialString.Split(',');
        foreach (var item in firstSplitStringsArray)
        {
            string [] secondlySplittedStringsArray = item.Split('=');
            finalList.Add(secondlySplittedStringsArray);
        }
    }
}

If you need only the first OU value, the code can be:

        string initialString = "CN=Test,OU=ABC,OU=Company,DC=CFLA,DC=domain";
        string requiredValue = string.Empty;

        string [] firstSplitStringsArray = initialString.Split(',');
        foreach (var item in firstSplitStringsArray)
        {
            string [] secondlySplittedStringsArray = item.Split('=');
            if (secondlySplittedStringsArray[0] == "OU")
            {
                requiredValue = secondlySplittedStringsArray[1];
                break;
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.