-2

I have this string and I need it split multiple ways.

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

That is the full string i need to split it as such

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

The Numbers are different every-time but i still need them to be split into there own strings. I have no idea how to go about this. Any help would be greatly appreciated!

What I've tried

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

It didn't seem to add the "|" to it.

dseds
  • 57
  • 1
  • 8

1 Answers1

1

Split the string on newlines then process each line based on the first word of that line. More info on splitting on newlines here... 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
  }
}

The code above is really just to get you started. You should probably create a class called PendingOrder with strongly typed properties for Price, Profit etc (e.g. use float or decimal for the numbers rather than strings) and pass the raw text in via the constructor to populate the properties.

Community
  • 1
  • 1
Quantumplate
  • 1,104
  • 8
  • 15