Capn Jack Capn Jack - 1 year ago 122
C# Question

How to parse a paragraph into an array c#?

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)

and I want to store into a string array like such:
string[] paragraph = { line1, line2, line3, etc..}
is there a faster way to do it than what I did above? Maybe an online resource that takes paragraphs and gives you a copy paste of a c# string array? Because the only way I see this being possible is by manually parsing each line by hand so I get something like:

string[] paragraph = { "MODULE Local (Parent := \"Local\",", "ParentModPortId := 1," , etc..}

and thats going to take forever because I've got like 50 of these modules to make.

EDIT: can someone fix my block quote so its formatted properly please?
EDIT2: To clarify the block of text mentioned after my first line is like a paragraph. I want it to be parsed after each line into the next slot of an array as shown in my example further down. I'm having an issue though because when creating a string variable if text is broken onto the next line it's no longer a part of that string. Look at this: at the end of the "MODULE Local (Parent := \"Local\", c# no longer assigns the text after that to my string..

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download