将值设置在字符串中的正确位置

时间:2016-10-20 11:08:57

标签: c# html string forms loops

我正在尝试构建一个字符串,并在字符串的正确位置设置值(0或1单选复选框)。它构建字符串并根据我需要从给定ID位置设置它的数组位置来设置值。 ID位置在int accessID

中设置
Response.Write(perm + " - " + Request.Form[perm] + "<br />");

此写输出为:

access_1 - 0
access_30 - 0
access_6 - 1
access_34 - 0
access_31 - 0
access_37 - 0
access_2 - 0
access_23 - 0
access_28 - 0
access_35 - 0
access_36 - 0

并且

Response.Write(inputPerms + "<br>");

写入将输出:

0#0#1#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#

access_6的值设置为1,因为在表格中我检查了第3行的无线电输入,因此它将其设置在字符串的第3个位置。它按此顺序工作,我试图根据访问ID位置在字符串中设置它。所以access_6需要在正确的位置设置1,这种情况应该是6,我该怎么做才能将值设置在字符串的正确位置。输出字符串应该是这样的:(access_6)第6个位置: -

0#0#0#0#0#1#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#

希望这很清楚,谢谢你的帮助。这是代码:

if (Request.Form["updatePerm"] != null && Request.QueryString["action"]=="update")
{
    foreach (string perm in Request.Form)
    {
        string input = perm.Substring(0, perm.IndexOf("_") + 1);

        if (input == "access_")
        {

            string accessRef = perm.Replace("access_", "");
            int accessID = Convert.ToInt32(accessRef) + 1;

            Response.Write(perm + " - " + Request.Form[perm] + "<br />");

            string chkInputs = Request.Form[perm];
            inputPerms += !string.IsNullOrWhiteSpace(chkInputs) ? chkInputs + "#" : "";

        }
    }

    Response.Write(inputPerms + "<br>");
}  

1 个答案:

答案 0 :(得分:0)

试试这个:

           string[] input = {
               "access_1 - 0",
               "access_30 - 0",
               "access_6 - 1",
               "access_34 - 0",
               "access_31 - 0",
               "access_37 - 0",
               "access_2 - 0",
               "access_23 - 0",
               "access_28 - 0",
               "access_35 - 0",
               "access_36 - 0"
                           };
            string inputPerms = "";
            foreach (string line in input)
            {
                inputPerms += string.Format("#{0}",line.Split(new char[] {'-'}).Skip(1).First().Trim());
            }
            Console.WriteLine(inputPerms);
            Console.ReadLine();