[英]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.但它给了我以下例外:'System.ArgumentException:'已添加具有相同键的项目',因为'OU'和'DC'被重复作为键。
What I want now is to split/convert/process this string into a List of string arrays (as shown above).我现在想要的是将此字符串拆分/转换/处理为字符串列表 arrays (如上所示)。 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. PS我有数百个这样的字符串,我只想要每个字符串中第一个“OU”的值。
If you really want a List of string- Array s, just omit the "ToDictionary" line.如果你真的想要一个字符串数组的列表,只需省略“ToDictionary”行。
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"会给你一个 Arrays 的列表,每个数组 [0] 保存“键”,[1] 保存“值”
ad PS: To get the first "OU" value, you can use a regular expression: ad PS:要获取第一个“OU”值,可以使用正则表达式:
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.您的表达式中有两个“OU”条目。 As a result, you cannot translate it to a dictionary.
因此,您无法将其翻译成字典。 Instead, translate it to a list of KeyValuePair
相反,将其转换为 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:如果只需要第一个 OU 值,代码可以是:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.