C# - 擦除字符串,如果在另一个字符串中找不到
我无法完成工作,它很近,但无法正常工作: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
}
}
}
I cannot get this to work, its close, but not working :D
My logic here seems to be a bit off, can someone help me out?
What i am trying to achieve is: check if string2 contains a word that does not exist in string1. if this kind of word is found, print it out, and delete it
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
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以切换到循环的
;请注意,您应该更改数组的项目,
string2 [i] =“”
,而不是循环变量:You can switch to
for
loop; note that you should change the item of the array,string2[i] = ""
, not loop variable: