-2

If I have a paragraph like this:

MODULE Local (Parent := "Local",
             ParentModPortId := 1,
              CatalogNumber := "1756-L71S",
              Vendor := 1,
              ProductType := 14,
              ProductCode := 158,
              Major := 27,
              Minor := 11,
              PortLabel := "RxBACKPLANE",
              ChassisSize := 13,
              Slot := 11,
              Mode := 2#0000_0000_0000_0001,
              CompatibleModule := 0,
              KeyMask := 2#0000_0000_0001_1111,
              SafetyNetwork := 16#0000_3acc_033e_6fa0)
END_MODULE

and it is currently saved as just a simple string in my program. Now, I'd like to save each line as an entry in an array, how would I go about doing that?

Capn Jack
  • 1,201
  • 11
  • 28
  • if the `,` is the one thing in common then why don't you split the data on the `,` – MethodMan Sep 21 '16 at 14:29
  • It's not clear at all what you're trying to achieve. Is the code you mention text input or something else? How is the input stored? Are you reading it off disk or from a database? There is so much information missing from your question it's almost impossible to answer. Take a look at the FAQ to see how to ask a good question. – Greg B Sep 21 '16 at 14:30
  • Please be more specific regarding what you want your end result to be. How do you want the individual strings in the array formatted? – Johan Strömhielm Sep 21 '16 at 14:38
  • @GregB I'm reading it off disk but I'm not sure how that helps also isnt that safe to assume as the reader? If I was using a server I would have asked about SQL or something along those lines in here. The person that edited my comment made the block of text im parsing into an array formatted like a block of code. It's just text. A big paragraph spaced out with the return key and it's causing problems when I try to put it into a string variable like so: https://gyazo.com/1aae0d19c02b6636d84fdd80bebb70c7. – Capn Jack Sep 21 '16 at 14:41

2 Answers2

0

Use String.Split function

string[] paragraph =yourString.Split(",");

Try to use double double quotes as escape char

String Para = @"MODULE Local (Parent := ""Local"",
         ParentModPortId:= 1,
          CatalogNumber:= ""1756-L71S"",
          Vendor:= 1,
          ProductType:= 14,
          ProductCode:= 158,
          Major:= 27,
          Minor:= 11,
          PortLabel:= ""RxBACKPLANE"",
          ChassisSize:= 13,
          Slot:= 11,
          Mode:= 2#0000_0000_0000_0001,
          CompatibleModule:= 0,
          KeyMask:= 2#0000_0000_0001_1111,
          SafetyNetwork:= 16#0000_3acc_033e_6fa0)
        END_MODULE";
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
  • The string is formatted in that block I posted above so if I just paste it like so: string paragraph = "//thatblock" then I get these errors see?@JaydipJ https://gyazo.com/1aae0d19c02b6636d84fdd80bebb70c7 – Capn Jack Sep 21 '16 at 14:33
  • It didn't work. https://gyazo.com/63bfe0c4fcc87b7165f6145efc6e4b07 My string is ending before 'Local' – Capn Jack Sep 21 '16 at 15:02
  • Oh wait you can use double double quotes to escape in @ right?? – Capn Jack Sep 21 '16 at 15:04
  • alright finally got it to work with that. Also I used (new string[] { Environment.NewLine }, StringSplitOptions.None) as my argument for splitting since I had commas on some lines that I needed. – Capn Jack Sep 21 '16 at 15:20
0

I observe that the format is fairly similar to JSON, albeit with other separators.

Hence, one way to solve it would be to convert it to JSON:

  • replace all ":=" with "="
  • replace ( and ) with { and }
  • replace , with ;
  • delete the MODULE labels

Then you can parse it using a JSON lib, for instance NewtonSoft, which is on NuGet.

If you know beforehand what string pairs to expect, you can cast it into a defined class. Otherwise, cast it into dynamic (or into a Dictionary if your .NET version is too old for dynamic) and continue on from there.

  • those values are very sensitive and are vital to these strings, I can't replace or change anything in them. – Capn Jack Sep 21 '16 at 14:35
  • How are they vital? They are separators, you can literally replace them with anything and then reverse the replacement to get the original when needed. – Johan Strömhielm Sep 21 '16 at 14:39