Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string as below.
set Bond_Type_Bond1 = 0 no_units VAR_E B1_B1_IMP_B1_PRM FULL_ND 0 1 0 0 1
set ProWedge_Type_Bond1 = 0 no_units VAR_E B1_B1_IMP_B1_PRM FULL_ND 0 1 0 0 1
set Bond1_Current_Offset = 0 mAmps USG_CURRENT B1_CU_PROC_RESP_NOWEDGE_B1_PRM FULL -50 50 0 -50 50

set Bond1_Force_Offset = 0.0 grams VAR_E B1_CU_PROC_RESP_NOWEDGE_B1_PRM FULL -50.0 50.0 0.0 -50.0 50.0
set Force_1 = 35.0 grams BFORCE BOND1_FORCE_PRM FULL 0.0 700.0 35.0 0.0 700.0
set Minimum_Force_1 = 5.0 grams BFORCE B1_CUREG_FORCE_LIFT_B1_PRM FULL_HIDDEN 0.0 20.0 5.0 0.0 20.0

I need to read the name and the values and put them in a list, I can read the name but when it comes to the values I cannot tell where they start from. Note what I need is the numbers at the end, there are 5 values but there can be more or less

What I have tried:

int paramNameFrom = s.IndexOf("set", StringComparison.OrdinalIgnoreCase) + "set".Length;
                        int paramNameTo = s.IndexOf("=") + "=".Length;
                        string paramName = s.Substring(paramNameFrom, paramNameTo - paramNameFrom);
                        paramName = paramName.Trim();

                        int paramValsFrom = s.IndexOf("PRM", StringComparison.OrdinalIgnoreCase) + s.IndexOf(" ");

                        string paramVals = s.Substring(paramValsFrom);

                        string[] refList = { };
                        refList = paramVals.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                        List<string> paramsValuesList = new List<string>();
                        foreach (string v in refList)
                        {
                            paramsValuesList.Add(v);
                        }

                        paramDict.Add(paramName, paramsValuesList);
Posted
Updated 30-Sep-18 23:18pm
v2

1 solution

Start by looking at your string: if it's six lines of text at you show, then the first thing to do is break it into separate lines.
If you are reading the data from a file, then the best way to do that would be to use File.ReadAllLines which returns an array of strings, one per line.
Otherwise, use string.Split to break it at the newline.

You can then process each line the same way.
Start by breaking it into "left" and "right" sections: string.Split using '=' will do that for you.
The left half will be "set " followed by the name, so that's simple: a basic SubString operation.
The right half is not too complex either: again use string.Split, with space as the separator, and discard empty entries.
The array you end up with will have a Length which tells you the number of entries, and if you ignore the first five, then the rest are the numbers you want, no matter how many of them there are.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900