我有以下字符串数据: -
mydata="KK,23,45,33,56,66,X/TT,01,23,23,56,Y/"
让我提醒一下,这个字符串数据可以是多个组,它由该字符/
分开。
确定。现在,就我而言,我需要从上面的组数据字符串中更具体地对其进行切片以推送它,同时循环这样的组数据: -
While Loop (Group i++)
'// first 3 items
等:" KK,23,45"发送到功能A
'// 2 items after that
等等:" 33,56"发送到功能B
'// last 2 items
等:" 66,X"发送到功能C
'// remove this charater "/" before continue next group data
i=i+1
Loop
我无法弄清楚如何在循环时像上面那样切片。你有任何想法来解决这个问题吗? 感谢您提前阅读并回复我的问题。
答案 0 :(得分:0)
看起来你可以从String.Split方法中受益:
答案 1 :(得分:0)
试一试:
string[] groups = mydata.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in groups)
{
string[] InnerGroup = item.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
SendToFunctionA(InnerGroup[0] + "," + InnerGroup[1] + "," + InnerGroup[2]);
SendToFunctionB(InnerGroup[3] + "," + InnerGroup[4]);
SendToFunctionC(InnerGroup[5] + "," + InnerGroup[6]);
}
或者通过While Loop:
string[] groups = mydata.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
int i = 0;
while(i < groups.Length)
{
string[] InnerGroup = groups[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
SendToFunctionA(InnerGroup[0] + "," + InnerGroup[1] + "," + InnerGroup[2]);
SendToFunctionB(InnerGroup[3] + "," + InnerGroup[4]);
SendToFunctionC(InnerGroup[5] + "," + InnerGroup[6]);
i++;
}
以上代码忽略空值。
如果组具有空值,则可以使用以下代码:
string[] groups = mydata.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
int i = 0;
while(i < groups.Length)
{
string[] InnerGroup = groups[i].Split(new char[] { ',' });
SendToFunctionA(InnerGroup[0] + "," + InnerGroup[1] + "," + InnerGroup[2]);
SendToFunctionB(InnerGroup[3] + "," + InnerGroup[4]);
SendToFunctionC(InnerGroup[5] + "," + InnerGroup[6]);
i++;
}
我希望这会有用。