C# - 查找/编辑/替换字符串

时间:2011-07-26 19:49:23

标签: c# replace richtextbox user-input addition

我正在尝试从文件中检索数据以更改它并替换旧数据。

我有一个看起来像这样的文件:

TEXT  NEXT_TEXT  10.505   -174.994 0  
TEXT  NEXT_TEXT  100.005  174.994  90  
TEXT  NEXT_TEXT  -10.000  -5.555   180  
TEXT  NEXT_TEXT  -500.987 5.123    270  
TEXT  NEXT_TEXT  987.123  1.000    180  
TEXT  NEXT_TEXT  234.567  200.999  90 

我正在尝试抓取第3和第4列,根据用户输入的两个TextBoxes来更改数据。让我们标记第3列 “X”第4列 “Y”。有了这个,还有两个文本框标记为“xTextBox”“yTextBox”。这些文本框允许用户输入数字,并且他们输入的数字(正数,负数和/或小数点后3位小数)每个文本框(X和Y)将 ADDED 到“X”列值和“Y”列值。

现在,我正在使用 CODE (下方)去除“X”和“Y”列,并将其值显示在不同的RichTextBoxes中标记为 xRichTextBox yRichTextBox

因此, xRichTextBox 如下所示:

10.505
100.005
-10.000
-500.987
987.123
234.567

yRichTextBox 如下所示:

-174.994
174.994
-5.555
5-123
1.000
200.999

CODE:

    private void calculateXAndYPlacementTwo()
    {
        // Reads the lines in the file to format.
        var fileReader = File.OpenText(filePath);

        // Creates a list for the lines to be stored in.
        var fileList = new List<string>();

        // Adds each line in the file to the list.
        while (true)
        {
            var line = fileReader.ReadLine();
            if (line == null)
                break;
            fileList.Add(line);
        }

        // Creates new lists to hold certain matches for each list.
        var xyResult = new List<string>();
        var xResult = new List<string>();
        var yResult = new List<string>();

        // Iterate over each line in the file and extract the x and y values
        fileList.ForEach(line =>
        {
            Match xyMatch = Regex.Match(line, @"(?<x>-?\d+\.\d+)\s+(?<y>-?\d+\.\d+)");
            if (xyMatch.Success)
            {
                // grab the x and y values from the regular expression match
                String xValue = xyMatch.Groups["x"].Value;
                String yValue = xyMatch.Groups["y"].Value;

                // add these two values, separated by a space, to the "xyResult" list.
                xyResult.Add(String.Join(" ", new[]{ xValue, yValue }));

                // Adds the values into the xResult and yResult lists.
                xResult.Add(xValue);
                yResult.Add(yValue);

                // Place the 'X' and 'Y' values into the proper RTB.
                xRichTextBox.AppendText(xValue + "\n");
                yRichTextBox.AppendText(yValue + "\n");
            }
        });
    }

SO 例如,如果用户输入“10.005”代表xTextBox“ - 20” yTextBox 已更新 xRichTextBox如下所示:

20.510
110.010
0.005
-490.982
997.128
224.572

并且已更新 yRichTextBox将如下所示:

-194.994
154.994
-25.555
-14.877
-19.000
180.999

发生这种情况后,我尝试用更新后的值替换原始文件的第3列和第4列

  • 我目前正在获取文件中的值(“X”和“Y”),并将它们输出为单独的RichTextBoxes,但我不知道如何将这些值添加到从每个{\ n}输入的值中{1}} ......我该怎么做?
  • 使用'TextBox`值计算TextBox值后,如何获取这些新值并将其添加到原始文件中?

**这些值并不总是相同,否则我会对它们进行硬编码。


所以,新文件看起来像这样:

RichTextBox

3 个答案:

答案 0 :(得分:1)

我建议使用结构化文本文件解析器来解析和重写文件。

Microsoft.VisualBasic.FileIO命名空间中有一个TextFieldParser。这是一个普通的.NET程序集,因此可以在代码中引用它并从C#中使用。

答案 1 :(得分:1)

您知道如何阅读文件,但听起来好像您只是将它们直接放入文本框中。我会创建一个对象来表示一条线(带有x,y,以及最后一列是什么)。当您阅读它们时,创建一个对象列表。要显示它们,请循环并选择每个x(如果您使用的是4.0,则可以使用linq轻松完成),并且每个y都可以选择单独的x。要更新,循环并向每个对象添加适当的数字。然后保存,再次写出每个对象。

编辑:仔细查看代码后,您将保留一份X列表和一份Y列表。创建我提到的对象,只保留一个列表。

答案 2 :(得分:0)

在保存日期之前,请务必创建要保存的数据字符串,并使用textwriter或streamwriter将所有内容再次保存到文件中。我认为你不能只改变文件的一部分并保存它

相关问题