比较行的字符串,并删除其中包含字线 [英] compare a string with lines and remove the line which contains words

查看:94
本文介绍了比较行的字符串,并删除其中包含字线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个txt文件包括:

  127.0.0.1 test69.com 
127.0.0.1 HTTP: //test69.com
127.0.0.1 ok.t​​k
127.0.0.1 man.test

如果我有字符串: test69.com man.test 与此文件来比较,可以在C#程序删除行(1)(2)(4)文件?


解决方案

 的String [] =域{test69.com,man.test}; 
的String [] =行File.ReadLines(文件名)
。凡(L =>!domains.Any(D => l.Contains(D)))
.ToArray( );

//写入线basck到文件,如果你需要
File.WriteAllLines(文件名,行);


So I have a txt file contains:

127.0.0.1    test69.com
127.0.0.1    http://test69.com
127.0.0.1      ok.tk
127.0.0.1 man.test

If I have the strings: test69.com and man.test to compare with this file , how could the c# program remove the line (1)(2)(4) in file?

解决方案

string[] domains = { "test69.com", "man.test" };
string[] lines = File.ReadLines(fileName)
                     .Where(l => !domains.Any(d => l.Contains(d)))
                     .ToArray();

// write lines basck to file, if you need
File.WriteAllLines(fileName, lines);

这篇关于比较行的字符串,并删除其中包含字线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆