C# 循环遍历字符串数组,每次获取每个项
所以我有一个数组:C# 循环遍历字符串数组,每次获取每个项,c#,asp.net,arrays,for-loop,C#,Asp.net,Arrays,For Loop,所以我有一个数组: string[] myArray = txtStudentIDList.Text.Split(new Char[] { ',' }); for (int i = 0; i < myArray.Length; i++) { var ice = new DAL(Context, "UpdateData"); ice.AddParam("ClientID", myArray[i]); } } 我希望此id
string[] myArray = txtStudentIDList.Text.Split(new Char[] { ',' });
for (int i = 0; i < myArray.Length; i++)
{
var ice = new DAL(Context, "UpdateData");
ice.AddParam("ClientID", myArray[i]);
}
}
我希望此id每次进入上面的for循环时都逐个获取值获取第一个值,然后下次进入循环时获取第二个值,这样它就可以根据给定的客户端id更新数据:例如:
// eg myrray has the values of "1234","12344"
1st time in the loop the id="1234" second time it comes in the look id = "12344"
现在的问题是,它将数组字符串中的所有内容放在一行中,如下所示:
id=1234\r\n12344
如何对其进行编码,使其不在一个字符串中,而是一个接一个地包含该值\r\n和所有值 似乎txtStudentIDList不是逗号分隔的列表,而是\r\n分隔的列表 更改您的第一行:
string[] myArray = txtStudentIDList.Text.Split(new Char[] { ',' });
致:
一旦myArray对象具有适当的值,此处的其余代码应按预期工作
这里找到了拆分方法:您需要使用您的语言。很难理解txtStudentIDList.Text的值是多少?拆分后myArray的内容是什么?什么是数组字符串?txtStudentIDList的示例是什么?每个myArray[i]由什么组成?您的代码看起来很好…您确定需要单独使用逗号拆分吗?通过使第一行字符串[]myArray=txtStudentIDList.Text.Splitnew字符串[]{\r\n},您似乎有一个意外的CRLF;上面写着不能从字符串转换成字符,我道歉。Split的定义需要两个参数,否则C认为您使用Splitchar[]的定义。追查edit@user7396598您不应该像您建议的那样逃离\r和\n。@NetMage谢谢。我已经从答案中删除了这一部分,以避免任何混淆。
string[] myArray = txtStudentIDList.Text.Split(new Char[] { ',' });
string[] myArray = txtStudentIDList.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);