C# 我怎样才能得到这些数字呢
我想在特定行之后获取值,我尝试了C# 我怎样才能得到这些数字呢,c#,C#,我想在特定行之后获取值,我尝试了if()(失败严重),所以我想知道在给定行之后是否有任何命令可以获取值?在这种情况下(sub1:…),还是使用给定的行从列表中查找整行 private void btnTransfer_Click(object sender, EventArgs e) { string Test1 = "@sub1:1001 \t @sub2:1002"; getValue(Test1); } public void
if()
(失败严重),所以我想知道在给定行之后是否有任何命令可以获取值?在这种情况下(sub1:…),还是使用给定的行从列表中查找整行
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;
}
}
}
使用前面提到的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;
}
}
}
public void getValue(字符串tekst)
{
char[]trim={'@','\t'};
IList myList=tekst.Split(“”).ToList();
foreach(myList中的字符串c)
{
字符串d=c.修剪(修剪);
如果(d.开始使用(“第1款”))
{
txtT1.Text=d;
}
如果(d.开始使用(“第2款”))
{
txtT2.Text=d;
}
}
}
该代码甚至无法编译。预期结果是什么?在对象的txtT1
和txtT2
的Text
成员中有1001
和1002
?我期望的结果是=sub:1001。输入一个文本框名txtT1。和sub:1002简介文本框txtT2@codor在空格处拆分后,只需在冒号处拆分,然后抓取[1]
数组,这应该是您的号码。使用d.StartsWith(“sub1”)
和d.StartsWith(“sub2”)