2016-01-19 55 views
-2

我有这个字符串,我需要它分割多种方式。在c中分割长字符串#

Pending order 
Sell EUR/USD 
Price 1.0899 
Take profit at 1.0872 
Stop loss at 1.0922 
From 23:39 18-01-2016 GMT Till 03:39 19-01-2016 GMT 

这是满弦,我需要将其分割为这样

string SellorBuy = "Sell"; 
string Price = "1.0889"; 
string Profit = "1.0872"; 
string StopLoss = "1.0922"; 

两个数字不同,每一次,但我仍然需要他们被分成有自己的字符串。我不知道如何去做这件事。任何帮助将不胜感激!

我已经试过

string message = messager.TextBody; 
message.Replace(Environment.NewLine, "|"); 
string[] Spliter; 
char delimiter = '|'; 
Spliter = message.Split(delimiter); 

它似乎没有添加 “|”到它。

+0

考虑使用'System.Text.RegularExpressions' – jacob

回答

1

在换行符上拆分字符串,然后根据该行的第一个字处理每行。对这里换行分裂的详细信息... https://stackoverflow.com/a/1547483/4322803

// Split the string on newlines 
string[] lines = theText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); 

// Process each line 
foreach(var line in lines){ 
    var words = line.Split(' '); 
    var firstWord = parts[0]; 

    switch (firstWord){ 
    case "Price": 
     Price = words[1]; 
     break; 
    case "Take": 
     Profit = words[words.Length - 1]; 
     break; 
    // etc 
    } 
} 

上面的代码实际上只是让你开始。您应该创建一个名为PendingOrder的类,其类型为PriceProfit等(例如,使用floatdecimal代替字符串),并通过构造函数传递原始文本以填充属性。

+0

正是我所期待的。 – dseds