【发布时间】:2015-11-17 11:28:43
【问题描述】:
我是 C# 的新手,我有一些问题。 我正在编写一些从远程机器获取以下输出的程序(使用 SSH):
wwn = 5001248018b6d7af
node_wwn = 5001248018b6d7ae
wwn = 5001248118b6d7af
node_wwn = 5001248118b6d7ae
wwn = 5001248218b6d7af
node_wwn = 5001248218b6d7ae
wwn = 5001248318b6d7af
node_wwn = 5001248318b6d7ae
上面的输出保存到字符串中...
我需要从这个输出列表或数组中提取以下格式:
50:01:24:80:18:b6:d7:af:50:01:24:80:18:b6:d7:ae
每两行是一对(wwn 和 node_wwn)
我值得以下功能
public void OutPutParse (string output)
{
string wwnn = null;
string wwpn = null;
string[] test = output.Split('\n');
test = test.Where(item => !string.IsNullOrEmpty(item)).ToArray();
//run all over the test array and exrract the wwns and wwpn
for (int i = 0; i < test.Length; i++)
{
}
}
这个函数创建一个 wwns 和 node_wwn 的数组(测试)
预期的结果是一个数组或列表,将像这样包含 wwn + node_wwn 50:01:24:80:18:b6:d7:af:50:01:24:80:18:b6:d7:ae
【问题讨论】:
-
看看 String.split 和 String.Format 基本上你可以把那个字符串拆分成一个数组。然后,如果需要,您可以使用另一个分隔符拆分数组中的项目......然后您可以使用 string.Format 进行输出。例如 - var myArray = MyString.Split(':'); String myNewString = String.Format("{0}{1}, myArray[0], myArray[1}");
-
能否请您为问题中的示例输入提供期望的输出?
-
@DmitryBychenko +1 因为 xx:xx:xx:yy:yy:yy 的列表并没有说太多看起来很奇怪
-
你有没有尝试过?这很简单,但我不愿意免费给你代码......
-
我用一些新的有用信息编辑了这个问题
标签: c# string parsing string-parsing