我想在特定行之后获取值,我尝试使用if()
(严重失败)所以我想知道在给定行之后是否有任何命令可以获取值?在这种情况下(sub1:...)或使用给定的行从List中找到整行?
private void btnTransfer_Click(object sender, EventArgs e)
{
string Test1 = "@sub1:1001 \t @sub2:1002";
getValue(Test1);
}
public void getValue(string tekst)
{
char[] trim = { '@','\t' };
myList = tekst.Split(' ').ToList();
foreach (string c in myList)
{
string d = c.Trim(trim);
if (d == "sub1")
{
txtT1.Text = d;
}
if (d == "sub2")
{
txtT2.Text = d;
}
}
}
答案 0 :(得分:0)
如上所述使用StartsWith应该可行。代码如下所示:
public void getValue(string tekst)
{
char[] trim = { '@', '\t' };
IList<string> myList = tekst.Split(' ').ToList();
foreach (string c in myList)
{
string d = c.Trim(trim);
if (d.StartsWith("sub1"))
{
txtT1.Text = d;
}
if (d.StartsWith("sub2"))
{
txtT2.Text = d;
}
}
}