c# - How do i filter and remove lines that don't contain the specifc word/s? -
this method now:
private void wordsfilter(list<string> newtext) { (int = 0; < newtext.count; i++) { (int x = 0; x < wordslist.words.length; x++) { linetopost = scrolllabel._lines[i]; if (!linetopost.contains(wordslist.words[x])) { newtext.remove(linetopost); } } } }
newtext list , worldslist.words string[]
i loop on lines in newtext , loop on words , want check way:
first line in newtext loop on words if none of of words exist in line remove current line , next line after it. example in newtext if line in index 0 : hello , line in index 1 is: created @ 12/3/2002 remove index 0 , index 1
index 2 empty space empty line not remove it.
then index 3 loop on words if nonoe of words exist in line in index 3 remove index 3 , index 4 .
and on...
how can ?
here working example. try didnt change logic of code :
using system; using system.collections.generic; public class program { public static void main() { list<string> list = new list<string>() {"truc", "i love toto", "next", "chocolate", "tata tata", "", "something"}; wordsfilter(list); } private static void wordsfilter(list<string> newtext) { string[] wordslist = new string[] { "toto", "tata" }; (int = 0; < newtext.count; i++) { (int x = 0; x < wordslist.length; x++) { if (newtext[i].contains(wordslist[x])) { newtext.removeat(i); if (i + 1 < newtext.count) newtext.removeat(i); } } } // print foreach(var item in newtext) { console.writeline(item); } } }
you should check how works foreach
loop , linq.
Comments
Post a Comment