Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析一个key1=";"富",;,键2=";酒吧;将CSV字符串放入字典(TKey、TValue的)_C#_.net_Vb.net_Csv_Dictionary - Fatal编程技术网

C# 解析一个key1=";"富",;,键2=";酒吧;将CSV字符串放入字典(TKey、TValue的)

C# 解析一个key1=";"富",;,键2=";酒吧;将CSV字符串放入字典(TKey、TValue的),c#,.net,vb.net,csv,dictionary,C#,.net,Vb.net,Csv,Dictionary,请使用以下字符串: cbr=“LACbtn”,detnumber=“1232700”,laclvetype=“ANN=x”,laccalcrun=“2014-09-10”,lacbutton=“Y”,lacaccdays=“0.00000000”,lacentdate=“2014-03-31”,lacotdays=“32.00”,laclastent=“2014-04-01”,lacsrvdays=“3,4”,status=“ok” 它的输出来自第三方SDK,我们需要访问.net应用程序中的值

请使用以下字符串:

cbr=“LACbtn”,detnumber=“1232700”,laclvetype=“ANN=x”,laccalcrun=“2014-09-10”,lacbutton=“Y”,lacaccdays=“0.00000000”,lacentdate=“2014-03-31”,lacotdays=“32.00”,laclastent=“2014-04-01”,lacsrvdays=“3,4”,status=“ok”

它的输出来自第三方SDK,我们需要访问.net应用程序中的值

在.net 2中有哪些对象可用于将此字符串解析到
字典(TKey,TValue)

例如,我们希望使用键名获取值,例如:

Dim X = Whatever("detnumber") '#### Where X would then contain "1232700"
我开始用逗号编写手动拆分的代码,但当引用的值中存在“,”&“=”时,这就变得很混乱,例如:
key1=“foo,bar”,key2=“hello=Dorothy!”
&我想一定已经存在某种东西来解析这种类型的字符串了

我只是找不到什么好运气

在vb.net或c#中提供建议都可以

字符串规则:

文档有点糟糕(即,我还不知道如何处理值内的双引号),但我可以确认以下几点:

  • 钥匙从不被引用
  • 键从不包含空格
  • 逗号可以存在于value:
    Key=“Hello:something”
  • 等号可以存在于value:
    Key=“something=something”
  • 值总是被引用,空值总是
    Key=”“
  • 一个键=一个值,一个键永远不会有多个值
  • 值之外不存在空白
  • 键是可变的,可以被称为任何东西,它们甚至可以存在于值中

好的,考虑到您对输入字符串格式有一些严格的规则,应该这样做:

public static Dictionary<string, string> GetInputKeyValuePairs(string input)
{
    var inputKeysAndValues = new Dictionary<string, string>();

    if (string.IsNullOrWhiteSpace(input)) return inputKeysAndValues;

    const char keyValueDelimiter = '=';
    const char itemDelimeter = ',';
    const char valueContainer = '"';

    var currentKey = string.Empty;
    var currentValue = string.Empty;
    var processingKey = true;
    var processingValue = false;

    foreach (var character in input)
    {
        if (processingKey)
        {
            // Add characters to currentKey until we get to the delimiter
            if (character == keyValueDelimiter) processingKey = false;
            else currentKey += character;
            continue;
        }
        if (processingValue)
        {
            // Add characters to the currentValue until we get to the delimeter
            if (character == valueContainer) processingValue = false;
            else currentValue += character;
            continue;
        }
        if (character == itemDelimeter)
        {
            // We're between items now, so store the current Key/Value, reset
            // them to empty strings, and set the flag to start processing a key.
            inputKeysAndValues[currentKey] = currentValue;
            currentKey = currentValue = string.Empty;
            processingKey = true;
            continue;
        }    
        if (character == valueContainer)
        {
            // We're at the first quote before a value, so ignore
            // it and set the flag to start processing a value
            processingValue = true;
            continue;
        }
    }

    // Add the last key/value
    if (currentKey != string.Empty) inputKeysAndValues[currentKey] = currentValue;

    return inputKeysAndValues;
}
公共静态字典GetInputKeyValuePairs(字符串输入)
{
var inputKeysAndValues=新字典();
if(string.IsNullOrWhiteSpace(input))返回inputKeysAndValues;
常量字符keyValueDelimiter='=';
常量字符itemDelimeter=',';
const char valueContainer='”;
var currentKey=string.Empty;
var currentValue=string.Empty;
var processingKey=true;
var processingValue=false;
foreach(输入中的变量字符)
{
if(处理键)
{
//将字符添加到currentKey,直到找到分隔符
如果(character==keyValueDelimiter)processingKey=false;
else currentKey+=字符;
持续
}
if(处理值)
{
//将字符添加到当前值,直到到达delimeter
如果(character==valueContainer)processingValue=false;
else currentValue+=字符;
持续
}
如果(字符==itemDelimeter)
{
//我们现在处于项目之间,因此存储当前键/值,重置
//将它们设置为空字符串,并设置标志以开始处理密钥。
inputKeysAndValues[currentKey]=currentValue;
currentKey=currentValue=string.Empty;
processingKey=true;
持续
}    
if(字符==valueContainer)
{
//我们在一个值之前的第一个引号处,所以忽略它
//单击并设置标志以开始处理值
processingValue=true;
持续
}
}
//添加最后一个键/值
如果(currentKey!=string.Empty)inputKeysAndValues[currentKey]=currentValue;
返回inputKeysAndValues;
}

您能给出输入接收方式的说明吗?关键部分是否从未引用过(也没有空格),值部分是否总是被引用,逗号周围是否从来没有空格,等等。您的原始示例代码应该包含有问题的字符串,以便人们理解“最坏情况”场景!值总是被引用?键总是有值?值总是有键?即字符串是否会是:
key1=value1,key2=,=“value1”“,“value3”
请同时显示您当前用于尝试解析这些值的代码。应该有助于提供更有用的答案。
var cbr=“LACbtn,detnumber=1232700,laclvetype=ANN=x,laccalcrun=2014-09-10,lacbutton=Y,lacaccdays=0.00000000,lacentdate=2014-03-31,lacotdays=32.00,laclastent=2014-04-01,lacsrvdays=3,4,status=ok”;var splitCbr=cbr.Split(新[]{',','='},StringSplitOptions.RemoveEmptyEntries)如果它始终是一致的,那么您可以执行以下操作,但是基于该字符串模式,很难确定这是否准确。这假设值不能包含引号,即:
funnyQuote=“他说,“有趣的事情”,key=“value”
这很有效-我后来确定值中的双引号被替换为制表符(我知道很奇怪),所以这不会成为问题。谢谢你的帮助!