 # Array - How to store multiple split up values in to array's [Answered]RSS

## 11 replies

Last post Jul 19, 2017 05:00 AM by gani7787

• ### Array - How to store multiple split up values in to array's

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"

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

my out put should come as below.

for example

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

so i can get below like

Sub1 = A+B

Sub2=D+E

Sub3=F+G

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

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

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

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

Sub1 = 90

Sub2 = 80

Sub3 = 90

• ### Re: Array - How to store multiple split up values in to array's

Any update pls...

• ### Re: Array - How to store multiple split up values in to array's

Jul 18, 2017 09:03 AM|Billy Liu|LINK

Hi gani7787,

I make a demo for you please try it.

```            string str = "Sub1 = A+B,Sub2=D+E,Sub3=F+G";
string[] words1 = str.Split(',');
Dictionary<string, int> TempSub = new Dictionary<string, int>();
for (int i = 0; i < words1.Length; i++)
{
int n = 0;
string[] words2 = words1[i].Split('='); // Again splitting the values for  Sub1 = A+B // Second split

string[] words3 = words2.Split('+');
for (int j = 0; j < words3.Length; j++)
{
switch (words3[j])
{
case "A":
n += 10;
break;
case "B":
n += 20;
break;
case "C":
n += 30;
break;
case "D":
n += 40;
break;
case "E":
n += 50;
break;
case "F":
n += 60;
break;
case "G":
n += 70;
break;
default:
n += 0;
break;
}
}
TempSub.Add(words2, n);  // So Sub1 will be the variables
}```

Best Regards,

Billy

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• ### Re: Array - How to store multiple split up values in to array's

Exactly 90% matching of my requirement.

What happened if Sub1 = A+B-C (if subtraction along with addition contains)..?

`string[] words3 = words2.Split('+'); ....????`

• ### Re: Array - How to store multiple split up values in to array's

Jul 18, 2017 09:49 AM|Billy Liu|LINK

Hi gani7787,

For example:

```string str = "Sub1 = A+B-C+D-A,Sub2=D+E,Sub3=F+G";
string[] words1 = str.Split(',');
Dictionary<string, int> TempSub = new Dictionary<string, int>();
for (int i = 0; i < words1.Length; i++)
{
int n = 0;
string[] words2 = words1[i].Split('='); // Again splitting the values for  Sub1 = A+B // Second split

string[] words3 = words2.Split('+');
for (int j = 0; j < words3.Length; j++)
{
int m = 0;
int sub = 0;
string[] words4 = words3[j].Split('-');
for(int k = 0; k < words4.Length; k++)
{
switch (words4[k].Trim())
{
case "A":
sub = 10;
break;
case "B":
sub = 20;
break;
case "C":
sub = 30;
break;
case "D":
sub = 40;
break;
case "E":
sub = 50;
break;
case "F":
sub = 60;
break;
case "G":
sub = 70;
break;
default:
sub = 0;
break;
}
if (k == 0)
{
m += sub;
}else
{
m -= sub;
}
}
n += m;
}
TempSub.Add(words2, n);  // So Sub1 will be the variables
}```

Best Regards,

Billy

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• ### Re: Array - How to store multiple split up values in to array's

Excellent Billy.

But, when we are returning the TempSub value, getting error.

How to return TempSub value as method or function.

See my full code.

double strReturnvalue = GetMarks(A,B,C,D,E,F,str); // Main and returns Tempsub value

public static double GetMarks(double A,double B,double C,double D,double E,double F,String str)

double retCondition = 0.0;
str = "Sub1 = A+B-C+D-A,Sub2=D+E,Sub3=F+G";
string[] words1 = str.Split('#');
Dictionary<string, double> TempSub = new Dictionary<string, double>();
for (int i = 0; i < words1.Length; i++)
{
double n = 0.0;
string[] words2 = words1[i].Split('='); // Again splitting the values for Sub1 = A+B // Second split

string[] words3 = words2.Split('+');
for (int j = 0; j < words3.Length; j++)
{
double m = 0.0;
double sub = 0.0;
string[] words4 = words3[j].Split('-');
for (int k = 0; k < words4.Length; k++)
{
switch (words4[k].Trim())
{
case "PIPE CL":
sub = 10;
break;
case "HALF OF PIPE DIA":
sub = 20;
break;
case "STEEL EL":
sub = 30;
break;
case "180":
sub = 180;
break;
default:
sub = 0;
break;
}
if (k == 0)
{
m += sub;
}
else
{
m -= sub;
}
}
n += m;
}
TempSub.Add(words2, n); // So Sub1 will be the variables
}

retCondition = TempSub ??????? // Error
return retCondition;

}

How to return TempSub value to method or function..?

• ### Re: Array - How to store multiple split up values in to array's

Jul 18, 2017 12:29 PM|Billy Liu|LINK

Hi gani7787,

The type of TempSub is Dictionary<string,double>, so please set the type of reCondition to Dictionary.

What value do you want? Is the average of the subs?

Best Regards,

Billy

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• ### Re: Array - How to store multiple split up values in to array's

i want to return

Tempsub all values as array or list into method or function (Getmarks)

TempSub value as below

 Sub1=30

 Sub2=90

 Sub=130

so my output returns all the above 3 values into StrReturnvalue as array or list

double strReturnvalue = GetMarks(A,B,C,D,E,F,str); // Main and returns Tempsub value

• ### Re: Array - How to store multiple split up values in to array's

Jul 18, 2017 01:03 PM|Billy Liu|LINK

Hi gani7787,

`Dictionary<string, double> TempSub = new Dictionary<string, double>();`

to

`List<string> TempSub =new List<string>();`

And change

`TempSub.Add(words2, n);`

to

`TempSub.Add(words2+"="+ n);`

Then the result of TempSub will be

TempSub = "Sub1=30"

TempSub = "Sub2=90"

TempSub ="Sub3=130"

Best Regards,

Billy

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• ### Re: Array - How to store multiple split up values in to array's

then how to assing tempsum value in to return variable.?

getting Error

Error : Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.IConvertible'.

retCondition = Convert.ToDouble(TempSub.ToList()); //Error

return retCondition;

Thanks.

• ### Re: Array - How to store multiple split up values in to array's

Jul 18, 2017 02:19 PM|Billy Liu|LINK

Hi gani7787,

What is retCondition?

The list can't be converted to a double.

If you want to returen the value of TempSub, you could try to change

`public static double GetMarks(double A,double B,double C,double D,double E,double F,String str)`

To

`public static List<string> GetMarks(double A,double B,double C,double D,double E,double F,String str)`

And use

`return TempSub;`

Best Regards,

Billy

MSDN Community Support