Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to store multiple split up values in to array's or collection or some other things.

For example,

I have below table value.


Student       Marks
Student1       Sub1 = A+B,Sub2=D+E,Sub3=F+G,etc
my marks column values like "Sub1 = A+B,Sub2=D+E,Sub3=F+G,etc"

I want to split All the subjects and store corresponding subject values in arrays.

my out put should come as below.

<pre>for example

string[] words1 = student1.Split(',');

so i can get below like

[0]Sub1 = A+B

[1]Sub2=D+E

[2]Sub3=F+G

  for (int i = 0; i < words1.Length; i++)
            {

              string[] words2 = words1[i].Split('='); // Again splitting the values for  Sub1 = A+B

                TempSub = words2[0];  // So Sub1 will be the variables
            }


So the output array would be in the name of "Sub1","Sub2","Sub3",etc..

Sub1[0] = 90 

Sub2[1] = 80

Sub3[2] = 90

pls. let me know if you need any more assist...


What I have tried:

string[] words1 = student1.Split(',');

so i can get below like

[0]Sub1 = A+B

[1]Sub2=D+E

[2]Sub3=F+G

  for (int i = 0; i < words1.Length; i++)
            {

              string[] words2 = words1[i].Split('='); // Again splitting the values for  Sub1 = A+B

                TempSub = words2[0];  // So Sub1 will be the variables
            }
Posted
Updated 17-Jul-17 23:02pm
v3

Try this:
string student1 = "Sub1 = A+B,Sub2=D+E,Sub3=F+G";
string[] subjects = student1.Split(',');
Dictionary<string, string> output = new Dictionary<string, string>();
foreach (string subject in subjects)
    {
    string[] details = subject.Split('=');
    if (details.Length == 2)
        {
        output[details[0].Trim()] = details[1].Trim();
        }
    }
 
Share this answer
 
Comments
gani7787 18-Jul-17 2:43am    
How to apply the formula.
For example,Input Parameter is
A=200
B=400
C=90
D=80
E=70
F=100
G=200
So the student1 like below.
string student1 = "Sub1 = A+B-C,Sub2=D+E-A,Sub3=F-B+G";



Sub1,Sub2,Sub3,etc value it may change in some other name in future. that's why i want to store as it is the name of left hande side variable.

So the final output is

String[] Subjects; // So subjects should be below value
So the final result should be for the subjects.

Sub1[0] = 510
Sub2[1] = -50
Sub3[2] = 500
OriginalGriff 18-Jul-17 3:30am    
That's a different question altogether!
To do that, you need to look at parameter substitutions, and expression evaluation.
This may help:
https://www.codeproject.com/Articles/241830/a-Tiny-Expression-Evaluator
Or this:
https://www.codeproject.com/Articles/2160/Runtime-C-Expression-Evaluator
try something like this.

var s = "Sub2=A+B,Sub2=D+E,Sub3=F+G";
           var a = s.Split(',').Select(x => x.Split('=')).ToArray();

           foreach (var tmp in a)
           {
               string sub=tmp[0].ToString();
               string mark = tmp[1].ToString();
           }
 
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