如果在另一个字符串中找不到该字符串,则擦除该字符串



我无法让它工作,它关闭了,但不工作:D我的逻辑似乎有点不对劲,有人能帮我吗?我试图实现的是:检查字符串2是否包含字符串1中不存在的单词。如果发现这种单词,请将其打印出来,然后删除

string[] string1 = { "1", "2", "3" };
string[] string2 = { "1", "2", "3", "hello" };
foreach (var var2 in string2)
{
foreach (var var1 in string1)
{
if (!var1.Equals(var2))
{
Consoleprint(var2); //print out the string that does not exist in string1[]... which is "hello"
var2.Replace(var2, ""); //erase the unmatched string
}
}
}

您可以切换到for循环;注意,您应该更改数组的string2[i] = "",而不是循环变量:

for (int i = 0; i < string2.Length; ++i)
if (!strings1.Contains(string2[i])) {
// Let's print array item before it will be "erased"
Consoleprint(string2[i]);
string2[i] = "";
}

相关内容

  • 没有找到相关文章

最新更新