我有一个

string CCstring = "CC01=50 CC02=300 CC03=500 CC04=40";


我想将各个值存储在单独的字符串中,例如:

for(int i = 0; i<=4; i++)
{
   string suffix = i.ToString().PadLeft(2, '0');
   string CCindividual = CCindividual + i;
   CCindividual = //THIS IS WHERE I WOULD LIKE TO GET MY INDIVIDUAL VALUES i.e 50,300,500,40;
    Console.WriteLn("CC" + i + " =" + CCIndividual);//Testing
}


我应该使用Regex或Substring进行哪种字符串操作。代码段的外观如何?

最佳答案

一条线:

string[] CCindividual = Regex.Split(CCstring, "CC[0-9]+=").Where(x => x != "").
    Select(x => x.Trim()).ToArray<String>();


不确定这是否是更有效的方法。

09-27 01:32