简体   繁体   English

C#编辑文件中的字符串-删除字符(000)

[英]C# Edit string in file - delete a character (000)

I am rookie in C#, but I need solve one Problem. 我是C#的新手,但我需要解决一个问题。 I have several text files in Folder and each text files has this structure: 我在Folder中有几个文本文件,每个文本文件都具有以下结构:

IdNr 000000100 编号000000100

Name Name 姓名姓名

Lastname Lastname 姓氏姓氏

Sex M 性爱M

.... etc... ....等等...

Load all files from Folder, this is no Problem ,but i need delete "zero" in IdNr, so delete 000000 and 100 leave there. 从Folder加载所有文件,这没有问题,但是我需要在IdNr中删除“零”,因此删除000000和100保留在那里。 After this file save. 此文件保存后。 Each files had other IdNr , Therefore, it is harder :( 每个文件都有另一个IdNr ,因此,:(

Yes, it is possible each files manual edit, but when i have 3000 files, this is not good :) Can C# one algorithm, which could this 000000 delete and leave only number 100? 是的,可以手动编辑每个文件,但是当我有3000个文件时,这不是很好:) C#可以使用一种算法,该算法可以删除这000000条并仅保留数字100吗?

Thank you All. 谢谢你们。 Vaclav 瓦茨拉夫

So, thank you ALL ! 所以,谢谢大家! But in the End I have this Code :-) : 但最后我有了这个代码:-):

using System.IO;

namespace name { public partial class Form1 : Form { public Form1() { InitializeComponent(); 命名空间名称{公共局部类Form1:表单{公共Form1(){InitializeComponent(); } }

    private void Browse_Click(object sender, EventArgs e)
    {
        DialogResult dialog = folderBrowserDialog1.ShowDialog();
        if (dialog == DialogResult.OK)
            TP_zdroj.Text = folderBrowserDialog1.SelectedPath;

    }

    private void start_Click(object sender, EventArgs e)
    {

       try
       {
           foreach (string file in Directory.GetFiles(TP_zdroj.Text, "*.txt"))
           {
               string text = File.ReadAllText(file, Encoding.Default);

               text = System.Text.RegularExpressions.Regex.Replace(text, "IdNr    000*", "IdNr    ");
               File.WriteAllText(file, text, Encoding.Default);

           }
       }
           catch
       {
           MessageBox.Show("Warning...!");
               return;

           }

        {
            MessageBox.Show("Done");

        }

    }
}

} }

Thank you ALL ! 谢谢你们 ! ;) ;)

You can use int.Parse : 您可以使用int.Parse

int number = int.Parse("000000100");
String withoutzeros = number.ToString();

According to your read/save file issue, do the files contain more than one record, is that the header or does each record is a list of key and value like "IdNr 000000100"? 根据您的读取/保存文件问题,文件是否包含多个记录,是标题还是每个记录都是键和值的列表,例如“ IdNr 000000100”? It's difficult to answer without these informations. 没有这些信息,很难回答。

Edit : Here's a simple but efficient approach which should work if the format is strict: 编辑 :这是一种简单但有效的方法,如果格式严格,则应该可以使用:

var files = Directory.EnumerateFiles(path, "*.txt", SearchOption.TopDirectoryOnly);

foreach (var fPath in files)
{ 
    String[] oldLines = File.ReadAllLines(fPath); // load into memory is faster when the files are not really huge
    String key = "IdNr ";
    if (oldLines.Length != 0)
    {
        IList<String> newLines = new List<String>();
        foreach (String line in oldLines)
        {
            String newLine = line;
            if (line.Contains(key))
            {
                int numberRangeStart = line.IndexOf(key) + key.Length;
                int numberRangeEnd = line.IndexOf(" ", numberRangeStart);
                String numberStr = line.Substring(numberRangeStart, numberRangeEnd - numberRangeStart);
                int number = int.Parse(numberStr);
                String withoutZeros = number.ToString();
                newLine = line.Replace(key + numberStr, key + withoutZeros);
                newLines.Add(line);
            }
            newLines.Add(newLine);
        }
        File.WriteAllLines(fPath, newLines);
    }
}

These are the steps you would want to take: 这些是您要采取的步骤:

  • Loop each file 循环播放每个文件
  • Read file line by line 逐行读取文件
  • for each line split on " " and remove leading zeros from 2nd element 对于在“”上分割的每一行,并从第二个元素中删除前导零
  • write the new line back to a temp file 将新行写回到临时文件
  • after all lines processed, delete original file and rename temp file 处理完所有行后,删除原始文件并重命名临时文件
  • do next file 做下一个文件

(you can avoid the temp file part by reading each file in full into memory, but depending on your file sizes this may not be practical) (您可以通过将每个文件全部读入内存来避免使用临时文件,但是根据文件大小,这可能不切实际)


You can remove the leading zeros with something like this: 您可以使用以下方式删除前导零:

string s = "000000100";
s = s.TrimStart('0');

使用TrimStart

var trimmedText = number.TrimStart('0');

This should do it. 这应该做。 It assumes your files have a .txt extension, and it removes all occurrences of "000000" from each file. 它假定您的文件具有.txt扩展名,并且从每个文件中删除所有出现的“ 000000”。

foreach (string fileName in Directory.GetFiles("*.txt"))
{
    File.WriteAllText(fileName, File.ReadAllText(fileName).Replace("000000", ""));
}

Simply, read every token from the file and use this method: 简单地,从文件中读取每个令牌并使用此方法:

var token = "000000100";
var result = token.TrimStart('0');

You can write a function similar to this one: 您可以编写与此函数类似的函数:

static IEnumerable<string> ModifiedLines(string file) {
    string line;
    using(var reader = File.OpenText(file)) {
        while((line = reader.ReadLine()) != null) {
            string[] tokens = line.Split(new char[] { ' ' });
            line = string.Empty;
            foreach (var token in tokens)
            {
                line += token.TrimStart('0') + " ";
            }
            yield return line;
        }
    }
}

Usage: 用法:

File.WriteAllLines(file, ModifiedLines(file));

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

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