Removing specified text from CSV file The 2019 Stack Overflow Developer Survey Results Are InHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How to concatenate text from multiple rows into a single text string in SQL server?How to output MySQL query results in CSV format?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Dealing with commas in a CSV fileGet int value from enum in C#Save PL/pgSQL output from PostgreSQL to a CSV fileHow to import CSV file data into a PostgreSQL table?Excel to CSV with UTF8 encodingPandas writing dataframe to CSV file

How to type this arrow in math mode?

Why is the maximum length of OpenWrt’s root password 8 characters?

Why was M87 targetted for the Event Horizon Telescope instead of Sagittarius A*?

Reference request: Oldest number theory books with (unsolved) exercises?

One word riddle: Vowel in the middle

Did Section 31 appear in Star Trek: The Next Generation?

How to deal with fear of taking dependencies

How to notate time signature switching consistently every measure

Return to UK after being refused entry years previously

Did 3000BC Egyptians use meteoric iron weapons?

Shouldn't "much" here be used instead of "more"?

Right tool to dig six foot holes?

Is three citations per paragraph excessive for undergraduate research paper?

Can a flute soloist sit?

How to answer pointed "are you quitting" questioning when I don't want them to suspect

"as much details as you can remember"

Why can Shazam fly?

Identify boardgame from Big movie

Why not take a picture of a closer black hole?

What are the motivations for publishing new editions of an existing textbook, beyond new discoveries in a field?

Are there incongruent pythagorean triangles with the same perimeter and same area?

How technical should a Scrum Master be to effectively remove impediments?

Have you ever entered Singapore using a different passport or name?

What is the motivation for a law requiring 2 parties to consent for recording a conversation



Removing specified text from CSV file



The 2019 Stack Overflow Developer Survey Results Are InHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How to concatenate text from multiple rows into a single text string in SQL server?How to output MySQL query results in CSV format?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Dealing with commas in a CSV fileGet int value from enum in C#Save PL/pgSQL output from PostgreSQL to a CSV fileHow to import CSV file data into a PostgreSQL table?Excel to CSV with UTF8 encodingPandas writing dataframe to CSV file



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















it's my first attempt at doing this and I have no idea if I'm on the right lines.



Basically I want to remove text from a CSV file that contains a specific keyword but I can't figure out how to remove the line.



 static void Main(string[] args)

var searchItem = "running";

var lines = File.ReadLines("C://Users//Pete//Desktop//testdata.csv");

foreach (string line in lines)

if (line.Contains(searchItem))

//Remove line here?












share|improve this question






















  • Would you like to remove whole line?

    – koviroli
    Mar 8 at 9:50

















2















it's my first attempt at doing this and I have no idea if I'm on the right lines.



Basically I want to remove text from a CSV file that contains a specific keyword but I can't figure out how to remove the line.



 static void Main(string[] args)

var searchItem = "running";

var lines = File.ReadLines("C://Users//Pete//Desktop//testdata.csv");

foreach (string line in lines)

if (line.Contains(searchItem))

//Remove line here?












share|improve this question






















  • Would you like to remove whole line?

    – koviroli
    Mar 8 at 9:50













2












2








2








it's my first attempt at doing this and I have no idea if I'm on the right lines.



Basically I want to remove text from a CSV file that contains a specific keyword but I can't figure out how to remove the line.



 static void Main(string[] args)

var searchItem = "running";

var lines = File.ReadLines("C://Users//Pete//Desktop//testdata.csv");

foreach (string line in lines)

if (line.Contains(searchItem))

//Remove line here?












share|improve this question














it's my first attempt at doing this and I have no idea if I'm on the right lines.



Basically I want to remove text from a CSV file that contains a specific keyword but I can't figure out how to remove the line.



 static void Main(string[] args)

var searchItem = "running";

var lines = File.ReadLines("C://Users//Pete//Desktop//testdata.csv");

foreach (string line in lines)

if (line.Contains(searchItem))

//Remove line here?









c# csv foreach






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 9:45









p.lowers2p.lowers2

132




132












  • Would you like to remove whole line?

    – koviroli
    Mar 8 at 9:50

















  • Would you like to remove whole line?

    – koviroli
    Mar 8 at 9:50
















Would you like to remove whole line?

– koviroli
Mar 8 at 9:50





Would you like to remove whole line?

– koviroli
Mar 8 at 9:50












4 Answers
4






active

oldest

votes


















0














The simple way if you'd like to remove a whole line:



 var searchItem = "running";
var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
var lines = File.ReadAllLines(pathToYourFile);
lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
File.WriteAllLines(pathToYourFile, lines);


For multiple search items:



 var searchItems = "running;walking;waiting;any";
var pathToYourFile = @"....items.csv";
var lines = File.ReadAllLines(pathToYourFile);
// split with your separator, actually is ';' character
foreach(var searchItem in searchItems.Split(';'))
lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

File.WriteAllLines(pathToYourFile, lines);





share|improve this answer

























  • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

    – p.lowers2
    Mar 8 at 10:10











  • It wouldn't matter, it's just to remove multiple words at once.

    – p.lowers2
    Mar 8 at 10:20











  • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

    – p.lowers2
    Mar 8 at 10:26











  • yes, I modified my answer with multiple search items, check it @p.lowers2

    – koviroli
    Mar 8 at 10:26












  • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

    – p.lowers2
    Mar 8 at 10:33


















1














Try this one to remove one or a few multiple words.



static void sd(string[] args)

string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
//string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



To remove multiple string, you can use this...



static void Main(string[] args)

string[] removeTheseWords = "aaa", "bbb", "ccc" ;

string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
string output = string.Empty;

foreach (string value in removeTheseWords)

output = contents.Replace(value, string.Empty);




More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace






share|improve this answer




















  • 1





    what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

    – er-sho
    Mar 8 at 10:32












  • @er-sho Updated. :)

    – DxTx
    Mar 8 at 10:43


















0














if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



for(int i=lines.Count - 1; i > -1; i--)

if (lines[i].Contains(searchItem))

lines.RemoveAt(i);







share|improve this answer
































    0














    You don't need to remove line just skip those lines that contain your search term



    foreach (string line in lines)

    if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

    //Do your work when line does not contains search term

    else

    //Do something if line contains search term




    Or alternative is to filtered your lines that does not contains your search term before loop like



    lines = lines.Where(line => !line.Contains(searchItem));

    foreach (string line in lines)

    //Here are those line that does not contain search term



    If your search term contains multiple words separated with comma(,) then you can skip those line by



    lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060515%2fremoving-specified-text-from-csv-file%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      The simple way if you'd like to remove a whole line:



       var searchItem = "running";
      var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
      File.WriteAllLines(pathToYourFile, lines);


      For multiple search items:



       var searchItems = "running;walking;waiting;any";
      var pathToYourFile = @"....items.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      // split with your separator, actually is ';' character
      foreach(var searchItem in searchItems.Split(';'))
      lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

      File.WriteAllLines(pathToYourFile, lines);





      share|improve this answer

























      • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

        – p.lowers2
        Mar 8 at 10:10











      • It wouldn't matter, it's just to remove multiple words at once.

        – p.lowers2
        Mar 8 at 10:20











      • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

        – p.lowers2
        Mar 8 at 10:26











      • yes, I modified my answer with multiple search items, check it @p.lowers2

        – koviroli
        Mar 8 at 10:26












      • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

        – p.lowers2
        Mar 8 at 10:33















      0














      The simple way if you'd like to remove a whole line:



       var searchItem = "running";
      var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
      File.WriteAllLines(pathToYourFile, lines);


      For multiple search items:



       var searchItems = "running;walking;waiting;any";
      var pathToYourFile = @"....items.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      // split with your separator, actually is ';' character
      foreach(var searchItem in searchItems.Split(';'))
      lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

      File.WriteAllLines(pathToYourFile, lines);





      share|improve this answer

























      • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

        – p.lowers2
        Mar 8 at 10:10











      • It wouldn't matter, it's just to remove multiple words at once.

        – p.lowers2
        Mar 8 at 10:20











      • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

        – p.lowers2
        Mar 8 at 10:26











      • yes, I modified my answer with multiple search items, check it @p.lowers2

        – koviroli
        Mar 8 at 10:26












      • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

        – p.lowers2
        Mar 8 at 10:33













      0












      0








      0







      The simple way if you'd like to remove a whole line:



       var searchItem = "running";
      var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
      File.WriteAllLines(pathToYourFile, lines);


      For multiple search items:



       var searchItems = "running;walking;waiting;any";
      var pathToYourFile = @"....items.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      // split with your separator, actually is ';' character
      foreach(var searchItem in searchItems.Split(';'))
      lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

      File.WriteAllLines(pathToYourFile, lines);





      share|improve this answer















      The simple way if you'd like to remove a whole line:



       var searchItem = "running";
      var pathToYourFile = @"C://Users//Pete//Desktop//testdata.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      lines = lines.Where(line => !line.Contains(searchItem)).ToArray();
      File.WriteAllLines(pathToYourFile, lines);


      For multiple search items:



       var searchItems = "running;walking;waiting;any";
      var pathToYourFile = @"....items.csv";
      var lines = File.ReadAllLines(pathToYourFile);
      // split with your separator, actually is ';' character
      foreach(var searchItem in searchItems.Split(';'))
      lines = lines.Where(line =>!line.Contains(searchItem)).ToArray();

      File.WriteAllLines(pathToYourFile, lines);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 8 at 10:26

























      answered Mar 8 at 9:56









      kovirolikoviroli

      683516




      683516












      • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

        – p.lowers2
        Mar 8 at 10:10











      • It wouldn't matter, it's just to remove multiple words at once.

        – p.lowers2
        Mar 8 at 10:20











      • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

        – p.lowers2
        Mar 8 at 10:26











      • yes, I modified my answer with multiple search items, check it @p.lowers2

        – koviroli
        Mar 8 at 10:26












      • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

        – p.lowers2
        Mar 8 at 10:33

















      • Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

        – p.lowers2
        Mar 8 at 10:10











      • It wouldn't matter, it's just to remove multiple words at once.

        – p.lowers2
        Mar 8 at 10:20











      • Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

        – p.lowers2
        Mar 8 at 10:26











      • yes, I modified my answer with multiple search items, check it @p.lowers2

        – koviroli
        Mar 8 at 10:26












      • Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

        – p.lowers2
        Mar 8 at 10:33
















      Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

      – p.lowers2
      Mar 8 at 10:10





      Hi, can I then modify this to say do multiple pharases? E..g. search item = "running", "walking"

      – p.lowers2
      Mar 8 at 10:10













      It wouldn't matter, it's just to remove multiple words at once.

      – p.lowers2
      Mar 8 at 10:20





      It wouldn't matter, it's just to remove multiple words at once.

      – p.lowers2
      Mar 8 at 10:20













      Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

      – p.lowers2
      Mar 8 at 10:26





      Perfect thanks! One last question, how would I go about numbering. Would it just be var searchItem = 0-9 ?

      – p.lowers2
      Mar 8 at 10:26













      yes, I modified my answer with multiple search items, check it @p.lowers2

      – koviroli
      Mar 8 at 10:26






      yes, I modified my answer with multiple search items, check it @p.lowers2

      – koviroli
      Mar 8 at 10:26














      Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

      – p.lowers2
      Mar 8 at 10:33





      Hi, so for example it will say in my csv "jogging":20192 and I just want to remove the numbers.

      – p.lowers2
      Mar 8 at 10:33













      1














      Try this one to remove one or a few multiple words.



      static void sd(string[] args)

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

      string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
      //string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



      To remove multiple string, you can use this...



      static void Main(string[] args)

      string[] removeTheseWords = "aaa", "bbb", "ccc" ;

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
      string output = string.Empty;

      foreach (string value in removeTheseWords)

      output = contents.Replace(value, string.Empty);




      More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace






      share|improve this answer




















      • 1





        what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

        – er-sho
        Mar 8 at 10:32












      • @er-sho Updated. :)

        – DxTx
        Mar 8 at 10:43















      1














      Try this one to remove one or a few multiple words.



      static void sd(string[] args)

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

      string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
      //string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



      To remove multiple string, you can use this...



      static void Main(string[] args)

      string[] removeTheseWords = "aaa", "bbb", "ccc" ;

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
      string output = string.Empty;

      foreach (string value in removeTheseWords)

      output = contents.Replace(value, string.Empty);




      More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace






      share|improve this answer




















      • 1





        what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

        – er-sho
        Mar 8 at 10:32












      • @er-sho Updated. :)

        – DxTx
        Mar 8 at 10:43













      1












      1








      1







      Try this one to remove one or a few multiple words.



      static void sd(string[] args)

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

      string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
      //string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



      To remove multiple string, you can use this...



      static void Main(string[] args)

      string[] removeTheseWords = "aaa", "bbb", "ccc" ;

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
      string output = string.Empty;

      foreach (string value in removeTheseWords)

      output = contents.Replace(value, string.Empty);




      More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace






      share|improve this answer















      Try this one to remove one or a few multiple words.



      static void sd(string[] args)

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");

      string output = contents.Replace("running", string.Empty).Replace("replaceThis", string.Empty).Replace("replaceThisToo", string.Empty);
      //string output = contents.Replace("a", "b").Replace("b", "c").Replace("c", "d");



      To remove multiple string, you can use this...



      static void Main(string[] args)

      string[] removeTheseWords = "aaa", "bbb", "ccc" ;

      string contents = File.ReadAllText("C://Users//Pete//Desktop//testdata.csv");
      string output = string.Empty;

      foreach (string value in removeTheseWords)

      output = contents.Replace(value, string.Empty);




      More info: https://docs.microsoft.com/en-us/dotnet/api/system.string.replace







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 8 at 10:42

























      answered Mar 8 at 10:30









      DxTxDxTx

      1,5711822




      1,5711822







      • 1





        what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

        – er-sho
        Mar 8 at 10:32












      • @er-sho Updated. :)

        – DxTx
        Mar 8 at 10:43












      • 1





        what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

        – er-sho
        Mar 8 at 10:32












      • @er-sho Updated. :)

        – DxTx
        Mar 8 at 10:43







      1




      1





      what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

      – er-sho
      Mar 8 at 10:32






      what if OP want to remove 100 search term? Is OP need to write .Replace function 100 times?

      – er-sho
      Mar 8 at 10:32














      @er-sho Updated. :)

      – DxTx
      Mar 8 at 10:43





      @er-sho Updated. :)

      – DxTx
      Mar 8 at 10:43











      0














      if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



      for(int i=lines.Count - 1; i > -1; i--)

      if (lines[i].Contains(searchItem))

      lines.RemoveAt(i);







      share|improve this answer





























        0














        if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



        for(int i=lines.Count - 1; i > -1; i--)

        if (lines[i].Contains(searchItem))

        lines.RemoveAt(i);







        share|improve this answer



























          0












          0








          0







          if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



          for(int i=lines.Count - 1; i > -1; i--)

          if (lines[i].Contains(searchItem))

          lines.RemoveAt(i);







          share|improve this answer















          if you are using foreach and removing from lines its will through an exception called collection modified exception so go with for



          for(int i=lines.Count - 1; i > -1; i--)

          if (lines[i].Contains(searchItem))

          lines.RemoveAt(i);








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 10:00

























          answered Mar 8 at 9:54









          AvinashAvinash

          17010




          17010





















              0














              You don't need to remove line just skip those lines that contain your search term



              foreach (string line in lines)

              if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

              //Do your work when line does not contains search term

              else

              //Do something if line contains search term




              Or alternative is to filtered your lines that does not contains your search term before loop like



              lines = lines.Where(line => !line.Contains(searchItem));

              foreach (string line in lines)

              //Here are those line that does not contain search term



              If your search term contains multiple words separated with comma(,) then you can skip those line by



              lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));





              share|improve this answer





























                0














                You don't need to remove line just skip those lines that contain your search term



                foreach (string line in lines)

                if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

                //Do your work when line does not contains search term

                else

                //Do something if line contains search term




                Or alternative is to filtered your lines that does not contains your search term before loop like



                lines = lines.Where(line => !line.Contains(searchItem));

                foreach (string line in lines)

                //Here are those line that does not contain search term



                If your search term contains multiple words separated with comma(,) then you can skip those line by



                lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));





                share|improve this answer



























                  0












                  0








                  0







                  You don't need to remove line just skip those lines that contain your search term



                  foreach (string line in lines)

                  if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

                  //Do your work when line does not contains search term

                  else

                  //Do something if line contains search term




                  Or alternative is to filtered your lines that does not contains your search term before loop like



                  lines = lines.Where(line => !line.Contains(searchItem));

                  foreach (string line in lines)

                  //Here are those line that does not contain search term



                  If your search term contains multiple words separated with comma(,) then you can skip those line by



                  lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));





                  share|improve this answer















                  You don't need to remove line just skip those lines that contain your search term



                  foreach (string line in lines)

                  if (!line.Contains(searchItem)) //<= Notice here I added exclamation mark (!)

                  //Do your work when line does not contains search term

                  else

                  //Do something if line contains search term




                  Or alternative is to filtered your lines that does not contains your search term before loop like



                  lines = lines.Where(line => !line.Contains(searchItem));

                  foreach (string line in lines)

                  //Here are those line that does not contain search term



                  If your search term contains multiple words separated with comma(,) then you can skip those line by



                  lines = lines.Where(line => searchItem.Split(',').All(term => !line.Contains(term)));






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 8 at 10:28

























                  answered Mar 8 at 9:50









                  er-shoer-sho

                  7,0502619




                  7,0502619



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55060515%2fremoving-specified-text-from-csv-file%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      1928 у кіно

                      Захаров Федір Захарович

                      Ель Греко