2015-11-17 26 views
-1

我用C#和新的和我有一些问题。 我正在写一些程序从远程机器下面的输出(使用SSH):解析多行的字符串(C#)

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++) 
     { 

     } 


    } 

该功能创建的WWN的阵列(测试)和node_wwn

预期的结果是一个数组或列出将包括WWN + node_wwn这样 50:01 :24:80:18:B6:D7:AF:50:01:24:80:18:B6:D7:AE

+1

看String.split 和 的String.Format 基本上你可以在字符串分割成一个数组。如果需要,可以使用另一个分隔符将数组中的项分开...... 然后,您可以使用string.Format输出。 例如 - var myArray = MyString.Split(':'); String myNewString = String.Format(“{0} {1},myArray [0],myArray [1}”); – AntDC

+1

您能否为问题中的样本输入提供*期望的输出*? –

+0

@DmitryBychenko +1因为xx:xx:xx:yy:yy:yy的列表并没有说太多,似乎很奇怪 – MajkeloDev

回答

0

最简单的方式实现这一目标是:

string www = "5001248018b6d7af"; 
string wwwn = "5001248018b6d7ae"; 

string myString = ""; 

// Implemenet loop here. 
// www = "number" // the next number you get 
// wwwn = "number" // the next number you get 
myString = www + myString + wwwn; 

你绝对当你有一个字符串列表或其他东西时,你必须用循环来完成它。

如果你想要它在同一个顺序,你可以做一个字符之间的字符就像一个“;”和分裂的所有号码,或者你只是做两个字符串,并在这样的结束将它们结合起来:

string www = "5001248018b6d7af"; 
string wwwn = "5001248018b6d7ae"; 
string mywwwString = ""; 
string mywwwnString = ""; 

// Implement the loop to add strings here 
// www = "number" // the next number you get 
// wwwn = "number" // the next number you get 
mywwwString += www; 
mywwwnString += wwwn; 
string myString = www + wwwn;