当前位置:编程学习 > C#/ASP.NET >>

判断字符串在数组中的位置

我现在有一个数组 yy={315,316,317,}
现在给出一个315,判断其再yy中的第几个,该如何判断 --------------------编程问答--------------------

int[] yy = { 315, 316, 317 };
                for (int i = 0; i < yy.Length; i++)
                {
                    if (yy[i] == 315)
                    {
                        //i+1就是你要的结果,也就是在yy中的第一个,y[i];
                    }

                }
--------------------编程问答-------------------- 循环,或者使用ling查找 --------------------编程问答-------------------- int [] yy new int []{315,316,317};
while(true)
{
if(315==0)
{
   Console.WriteLine("315的位置果然是0");
}
} --------------------编程问答-------------------- int index = Array.IndexOf(yy,"315"); --------------------编程问答-------------------- 这题目是基础题目啊大哥,自己好好想想! --------------------编程问答-------------------- 居然有用循环的... --------------------编程问答--------------------
int[] yy = new int[] { 315, 316, 317 };
int n = 315;
int index = Array.IndexOf(yy, n);
--------------------编程问答--------------------
引用 4 楼 ojlovecd 的回复:
int index = Array.IndexOf(yy,"315");


用indexOf直接找索引位置。。。用循环那就有点没事找事了。。

--------------------编程问答-------------------- 遍历循环,然后判断相等。

static void Main()
{
   int [] list = {315,316,317};
   //遍历
   for(int i = 0;i < list.Lenght;i ++)
  {
       if(list[i] == 315)
      {
          Console.Write("315在该数组中第" + (i + 1) + "个位置");
      }
  }
}
--------------------编程问答-------------------- --------------------编程问答-------------------- int index = Array.IndexOf(yy,"315");
--------------------编程问答--------------------
引用 6 楼 vrhero 的回复:
居然有用循环的...

”居然“要改成“必然” --------------------编程问答-------------------- 我现在要做成这样,哪里有错呢,该怎么改
string syjh="";
int index=0;
for(int j=0;j<gridview2.rows.count;j++)
{
syjh=gridview2.rows[j].cells[1].text+",";

}
for(int i=0;i<gridview1.rows.count;i++)
{
string jihuaID=gridview1.rows[i].cells[1].text;
int k=syjh.indexof(jihuaID)

index=k+1;
}


} --------------------编程问答-------------------- 看的我蛋疼 --------------------编程问答-------------------- 后台代码:
   using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProgramZn2.NewFolder_new
{
    public partial class xuhaobianzhi : System.Web.UI.Page
    {

        ShiYanJiLuDataContext jilu = new ShiYanJiLuDataContext();
        string tem = "";
        int index = 0;

        string str = "";


        protected void Page_Load(object sender, EventArgs e)
        {

            bindtree();
            bindtree2();
        }


        public  void  bindtree()
        {
        
       this.GridView1.DataSource=from d in jilu.test_plans
                                 where d.试验单号==1
                                 orderby d.计划ID
                                 select d; 
            this.GridView1.DataBind();

            string syjh = "";
           

            for (int j = 0; j < GridView1.Rows.Count; j++)
            {
                syjh = GridView1.Rows[j].Cells[1].Text + ",";


                str = str + syjh;
            }
        
        }


        public void bindtree2()
        {

            this.GridView2.DataSource = from d in jilu.test_tasks
                                        where d.试验单号 == 1
                                        orderby d.试验计划号
                                        select d;
            this.GridView2.DataBind();


        }

      public string  get_xuhao(object jihuaID)
        {


            if (tem == "")
            {
                index++;
                return index.ToString();

            }
            else
            {

                if (tem != jihuaID.ToString())
                {
                    index++;
                    tem = jihuaID.ToString();

                    return index.ToString();

                }

                else
                {

                    return index.ToString();
                
                }
            }

        }



      public string get_renwuhao(object jihuaID)
      {


          for (int i = 0; i < GridView2.Rows.Count; i++)
          {
              string jihua = GridView2.Rows[i].Cells[1].Text;

              //Response.Write(str);
              Response.Write(jihua);

              int k = str.IndexOf(jihua, 0);

              index = k + 1;

             // Response.Write(k);

          }

          return index.ToString();

      }




    }
}


前台代码


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            Width="661px">
            <Columns>
                <asp:TemplateField HeaderText="序号">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%#get_xuhao(Eval("计划ID"))%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="计划ID" HeaderText="计划ID" />
                <asp:BoundField DataField="试验计划" HeaderText="试验计划" />
            </Columns>
        </asp:GridView>
        <br />
        <br />
        <br />
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
            Width="667px">
            <Columns>
                <asp:TemplateField HeaderText="序号">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%#get_renwuhao(Eval("试验计划号"))%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="试验计划号" HeaderText="试验计划号" />
                <asp:BoundField DataField="实验任务" HeaderText="实验任务" />
            </Columns>
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>




就是想实现下面的效果 (但以上的代码不能实现,我想知道错在哪里了,怎么改,请各位大虾指教,非常感谢)
Gridview1显示如下

序号 计划ID 试验计划
1 3 ffffff
2 4 ffffff
3 5 nananana


Gridview2显示如下
序号 试验计划号 实验任务
1 3 jjjj
2 4 ffff
2 4 gggggg
3 5 nanan999
3 5 333
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,