简体   繁体   English

从 .txt 文件中删除特定单词 C#

[英]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.所以我一直在开发一个控制台库存系统,它可以选择添加/删除东西并将其保存在 a.txt 文件中。 I've managed to add things to a.txt file and save it, but I am unable to delete specific words from.txt file.我已设法将内容添加到 a.txt 文件并保存,但我无法从 .txt 文件中删除特定单词。 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!我是 C# 的新手,所以我对此知之甚少,因此不胜感激!

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:我忘了提到的是,例如,当我将数量为 100 的库存(文本文件)项目代码 AP01 添加到我的文本文件中时,它将具有以下内容:

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'.现在我想要实现的是,在我输入我的 itemCode 以从文本文件中删除项目之后,我试图要求用户输入“数量”。 So for example if I put itemCode as AP01 (to remove), it will then ask me to input a value, for example 25.例如,如果我将 itemCode 设置为 AP01(删除),它会要求我输入一个值,例如 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.执行List<T>.Remove()只会从列表中删除条目,而不一定是单词。 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':您可以编写以下代码来删除“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: Output 将是:

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:如果您想删除包含您的商品代码的行,请将newLine替换为:

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

Which the output would be output 将是哪个

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:根据您的编辑,您可能希望创建一个 model 来表示文本文件中的项目,如下所示:

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.这将减少数量,如果它们为 0 或更少,则将它们从文本文件中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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