Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#哈希表搜索_C#_Search_Hashtable - Fatal编程技术网

C#哈希表搜索

C#哈希表搜索,c#,search,hashtable,C#,Search,Hashtable,我需要搜索一些数据。首先,我的代码选择第29位并保留其中的4位(如您所见,下面是1721),然后比较下面的行。我设法搜索了第29个4位数字,并显示了消息,因为您可以看到行中是否有搜索到的数字,这是简单的部分。这是我的问题;当我搜索号码时,如何使其显示标签(label9、label10、label11、label12)上的前4位数字、后3位数字、第3位6位数字、第4位6位数字。我试图currentLine.Substring(1,4)但它显示了一个错误: 子字符串为空 我是否需要//搜索if部件中

我需要搜索一些数据。首先,我的代码选择第29位并保留其中的4位(如您所见,下面是1721),然后比较下面的行。我设法搜索了第29个4位数字,并显示了消息,因为您可以看到行中是否有搜索到的数字,这是简单的部分。这是我的问题;当我搜索号码时,如何使其显示标签(label9、label10、label11、label12)上的前4位数字、后3位数字、第3位6位数字、第4位6位数字。我试图
currentLine.Substring(1,4)但它显示了一个错误:

子字符串为空

我是否需要//搜索if部件中的循环

例如,假设我们将1723放在搜索中,它必须在label9上显示1097,在label10上显示003,等等

数据:

1096:001:161207:085050:1721:001:F:000:0007       
1096:001:161207:085050:1721:001:F:000:0007           
1099:003:161207:085719:1722:001:F:000:0007       
1099:003:161207:085719:1722:001:F:000:0007      
1097:002:161207:085719:1723:001:F:000:0007       
1097:002:161207:085719:1723:001:F:000:0007     
代码:

public partial class Form1 : Form
{
   public Form1()
    {
        InitializeComponent();
    }

    String currentItemIndex = "", currentItemData = "", currentLine = "";
    Hashtable hashtable = new Hashtable();

    private void button1_Click(object sender, EventArgs e)
    {
        //Select File
        openFileDialog1.ShowDialog();
        textBox1.Text = openFileDialog1.FileName;
        //Select File

        //Read And Split
        FileInfo file = new FileInfo(openFileDialog1.FileName.ToString());
        StreamReader read = file.OpenText();

        currentLine = read.ReadLine();
        currentItemIndex = currentLine.Substring(23, 4);
        currentItemData += currentLine;
        do
        {
            currentLine = read.ReadLine();
            if (currentLine == null)
            {
                hashtable.Add(currentItemIndex, currentItemData);
                break;
            }

            if (!currentItemIndex.Equals(currentLine.Substring(23, 4)))
            {
                hashtable.Add(currentItemIndex, currentItemData);
                currentItemData = "";
                currentItemIndex = currentLine.Substring(23, 4);
            }


            currentItemData += currentLine;
        } while (true);
    }


    private void button2_Click(object sender, EventArgs e)
    {
        //Search Start
        string search = textBox2.Text;
        if (hashtable.ContainsKey(search))
        {
            MessageBox.Show("Found");
            label9.Text=
            label10.Text=
            label11.Text=
        }
        else
        {
            MessageBox.Show("NotFound"); }
            //Search End
        }
    }
}

我发现这里的解决方案适合好奇的人:

        string search = textBox2.Text;
        if (hashtable.ContainsKey(search))

        {

            this.Size = new Size(324, 260);


            string value1 = (string)hashtable[search];
            label9.Text= value1.Substring(0, 4);
            label10.Text = value1.Substring(5, 3);
            label11.Text = value1.Substring(9, 6);
            label12.Text = value1.Substring(16, 6);
            label13.Text = value1.Substring(23, 4);
            MessageBox.Show("Found");

        }
        else
        {
            MessageBox.Show("NotFound");

        }

除此之外,您使用的是
Hashtable
而不是
Dictionary
,还有什么原因吗?自2005年以来,非泛型集合对于新代码来说已经有些过时了…您可能最好根据
将字符串拆分为一个值数组。实际上,我不知道如何使用
字典
如果他有大量数据,他可能需要哈希表,字典的速度稍慢,您还必须确保所有字典键在数据中都是唯一的。但他可能会考虑在这里查字典: