0
我需要在某些数据中进行搜索。首先,我的代码选择了第29位数字并保留它的4位数字(它在下面的1721中,你可以看到),它比较下面的几行。我设法搜索第29位4位数字并显示消息,因为您可以看到行中是否有搜索到的数字,这很容易。这是我的问题;当我搜索该号码时,如何使它显示标签上的前4位数字,后3位数字,后6位数字,后6位数字(label9,label10,label11,label12)。我试图currentLine.Substring(1, 4);
但它显示了一个错误:C#哈希表搜索
substring is null.
我是否需要,如果部分//搜索一个循环?
例如,假设我们把1723的搜索,它必须显示1097上label9,它必须显示003 label10,等
数据:
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
}
}
}
除了别的,你有什么理由使用'Hashtable'而不是'Dictionary <,>'?自2005年以来,非泛型集合已经有些过时了 - 对于新代码... –
您可能会更好地将字符串拆分为基于':'的值数组。 – JuanR
其实我不知道如何使用'Dictionary' – Osakawa