【发布时间】:2018-03-01 20:53:55
【问题描述】:
我目前正在将带有多个分隔符的字符串传递给我想最初将 (at ~) 拆分为字符串数组的方法,然后将数组中的每个条目 (at |) 传递给字典 ("key "="值")。
~timestamp=Mar 1 2018 3:14PM|proc=procedure A|tran=transaction 1|rowCount=987
~timestamp=Mar 1 2018 3:14PM|proc=procedure B|tran=transaction 2|rowCount=654
~timestamp=Mar 1 2018 3:14PM|proc=procedure C|tran=transaction 3|rowCount=321
我可以完成初始拆分为单独的“行”
~timestamp=Mar 1 2018 3:14PM|proc=procedure A|tran=transaction 1|rowCount=987
与:
string result = <<long string>>;
string filepath = <<filepath variable>>;
string[] resultRows = result.Split('~');
try
{
foreach (string row in resultRows)
{
if (!File.Exists(filepath))
{
using (StreamWriter log = File.CreateText(filepath))
{
log.WriteLine(row);
}
}
else
{
using (StreamWriter log = File.AppendText(filepath))
{
log.WriteLine(row);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
但是,当我尝试将这些“行”中的每一个解析为字典时,会引发错误(索引超出数组的范围)。
string result = <<long string>>;
string filepath = <<filepath variable>>;
string[] resultRows = result.Split('~');
Dictionary<string, string> dict = new Dictionary<string, string>();
try
{
foreach (string row in resultRows)
{
var results = row.Split('|').Select(s => s.Split(new[] { '=' }));
foreach (var item in results)
{
dict.Add(item[0], item[1]);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
我假设我对字典的期望不正确 - 对于字典中的每个“条目”,我希望看到以下内容:
~timestamp=>Mar 1 2018 3:14PM, proc=>procedureA, tran=>transaction1, rowCount=>987
这样我就可以将每一行写入一个日志文件,在那里我可以单独访问每个键的值。如果我对如何在字典中设置/获取值的理解还差得很远,有没有更好的方法来完成这项任务?
【问题讨论】:
-
抛出时
item.Length是什么? -
我相信我“修复”了显示输入行,使得每一行都是统一的,并且显示时没有换行,请验证确实如此。
-
您的数据以“~”开头。你认为
"~something".Split('~');的结果是什么?如果你再次用“|”分割每个分割字符串部分,结果是什么......?使用调试器,看看会发生什么。 (提示:StringSplitOptions.RemoveEmptyEntries) -
您必须删除第一个“~”。它返回一个空行
-
键是唯一的,因此您不能拥有多个名为“proc”或“timestamp”的键。请详细描述在处理文件中的前五个“行”之后字典的实际外观(每个
KeyValuePair)。如果您只想将每一行写入日志文件,也许您需要一个List<CustomClass>,而您的CustomClass将具有Timestamp、Procedure、Transaction和RowCount的属性。跨度>
标签: c# arrays dictionary