简体   繁体   中英

Splitting a string into a List of string array in C#

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.

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

If you really want a List of string- Array s, 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];

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();

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;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM