我一直在编码这个小项目。问题是文本框中有不止一个,并且只添加了一次“ a”。我不明白为什么?你们推荐我更改我的代码吗?
if (textBox3.Text.Contains("LKFKEOFK"))
{
textBox4.AppendText("a");
}
答案 0 :(得分:2)
int i = 0;
while ((i = textBox3.Text.IndexOf("LKFKE0FK", i, StringComparison.CurrentCulture)) != -1)
{
i += "LKFKE0FK".Length;
textBox4.AppendText("a");
}
编辑:
List<Tuple<int, string>> tupleList = new List<Tuple<int, string>>();
int i = 0;
while ((i = textBox3.Text.IndexOf("LKFKE0FK", i, StringComparison.CurrentCulture)) != -1)
{
i += "LKFKE0FK".Length;
tupleList.Add(new Tuple<int, string>(i, "a"));
}
int j = 0;
while ((j = textBox3.Text.IndexOf("LDMWICB", j, StringComparison.CurrentCulture)) != -1)
{
j += "LDMWICB".Length;
tupleList.Add(new Tuple<int, string>(j, "b"));
}
tupleList.Sort();
for (int k = 0; k < tupleList.Count; k++)
{
textBox4.AppendText(tupleList.ElementAt(k).Item2);
}
答案 1 :(得分:0)
textBox4.AppendText(new string('a', Regex.Matches(textBox3.Text, "LKFKEOFK").Count));
或者,如果您想为每次运行添加一个完整的字符串
textBox4.AppendText(string.Concat(Enumerable.Repeat("ab", Regex.Matches(textBox3.Text, "LKFKEOFK").Count)));
要求您的LKFKEOFK不包含任何RegEx relevant characters