简体   繁体   中英

Delete specific word from .txt file C#

So I've been working on a Console inventory system that would have options to add/remove things and save it in a.txt file. I've managed to add things to a.txt file and save it, but I am unable to delete specific words from.txt file. The code that I've attached below is supposed to remove the word that was saved in the string from the text file that I've opened up. But after running the code and checking if it's there or not, I found out that it is still there. I am new to C# so I haven't got much knowledge about this so any help would be appreciated!

static void RemoveStock()
{
    Console.WriteLine("==============================");
    string filePath = @"C:\Users\Ibrah\OneDrive\Desktop\SDAM\Stock Management\Stock Management System\Intentory System\database\Stock.txt.txt";
    List<string> lines = new List<string>();
    lines = File.ReadAllLines(filePath).ToList();

    Console.WriteLine("Enter the item code you'd like to remove: ");
    string itemCode = Console.ReadLine();
    lines.Remove(itemCode);
    File.WriteAllLines(filePath, lines);

}

Also, after fixing the above. What I forgot to mention was that for example, when I add to my inventory (text file) item code AP01 with a quantity of 100. It'll have the following in my text file:

AP01 100

Now what I'm trying to achieve is that after I put in my itemCode for removing the item from the text file, I'm trying to ask the user for an input of 'quantity'. So for example if I put itemCode as AP01 (to remove), it will then ask me to input a value, for example 25.

So this

AP01 100

would become this

AP01 75

here's my updated code after I got my answer:

    Console.WriteLine("==============================");
    string filePath = @"C:\Users\Ibrah\OneDrive\Desktop\SDAM\Stock Management\Stock Management System\Intentory System\database\Stock.txt.txt";
    List<string> lines = new List<string>();
    lines = File.ReadAllLines(filePath).ToList();

    Console.WriteLine("Enter the item code you'd like to remove: ");
    string itemCode = Console.ReadLine();
    Console.WriteLine("Enter the amount that you'd like to remove: ");
    string itemQuantity = Console.ReadLine();

    var newLines = lines.Where(line => !line.Contains(itemCode, StringComparison.OrdinalIgnoreCase));
    File.WriteAllLines(filePath, newLines);

Doing a List<T>.Remove() will only remove the entry from the list, and not necessarily the word. This is under the assumption that the text file has more than the item code in the line. Assuming the text file looked something like this:

Hello world

Yes, what a lovely day it is to say hello

Good day

Than you can write the following code to remove 'Hello':

string filePath = @"path\to\file.txt";
var lines = File.ReadAllLines(filePath);
string itemCode = "Hello";
// If you don't want the replace to be case insensitive, do line.Replace(itemCode, string.Empty)
var newLines = lines.Select(line => Regex.Replace(line, itemCode, string.Empty, RegexOptions.IgnoreCase));
File.WriteAllLines(filePath, newLines);

Output would be:

world

Yes, what a lovely day it is to say

Good day

If you're wanting to remove the line that has your item code, replace the newLine with:

var newLines = lines.Where(line => !line.Contains(itemCode, StringComparison.OrdinalIgnoreCase));

Which the output would be

Good day

Edit

Based on your edit, you'll likely want to create a model to represent the items in your text file, like so:

public class Item
{
    public Item(string input)
    {
        var splitInput = input.Split(' ');
        Code = splitInput[0];
        Qty = int.Parse(splitInput[1]);
    }

    public Item(string code, int qty)
    {
        Code = code;
        Qty = qty;
    }

    public string Code { get; }
    public int Qty { get; }

    public override string ToString()
    {
        return $"{Code} {Qty}";
    }
}

And the code would look something like:

static void Main(string[] args)
{
        
    string filePath = @"path\to\file.txt";
    var lines = File.ReadAllLines(filePath).Select(line => new Item(line));

    string itemCode = "AP01";
    int qty = 25;

    var newLines = lines
        .Select(item => item.Code == itemCode ? new Item(item.Code, item.Qty - qty) : item)
        .Where(item => item.Qty > 0)
        .Select(item => item.ToString());

    File.WriteAllLines(filePath, newLines);
}

This will reduce the qty down, and if they're 0 or less, remove them from the text file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM