Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys I want to use replace in a single line of code using Linq

my ListBoxItem contains this words

MEA+AAE+VGM+KGM:16795'
FTX+AAI+++?CONTAINERSTOBESTOWEDAWAYFROMHEATANDTOBESTOREDUNDERDECK?'
TDT+1++3++688888:172:87+++:172:87'


and I want to replace the character '?' and place the new lines
in File.WriteAllLines(filePath, newLines);

What you see on the below code is a method to delete the whole selected line

I want to change that to a method that can replace the question mark sign'?'
using linq

C#
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines) // Read all line from the text file  
{
   oListBox.Items.Add(line);
}
foreach (var listBoxItem in oListBox.Items)
{
  if (listBoxItem.ToString().Contains("?"))
  {
     string word = listBoxItem.ToString();
     var oldLines = File.ReadAllLines(filePath);
     var newLines = oldLines.Where(line => !line.Contains(word));//delete the selected line  
     
     File.WriteAllLines(filePath, newLines);
     FileStream oFileStream = new FileStream(filePath, FileMode.Append);
     oFileStream.Close();
   }
}


What I have tried:

I still trying to experiment different code using linq
Posted
Updated 9-Jun-21 19:40pm
v2

1 solution

You need to use Split[^] function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900