如何将多个拆分值存储到数组中 [英] 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"
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
}
推荐答案
试试这个:
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();
}
}
尝试这样的事情。
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();
}
这篇关于如何将多个拆分值存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文