【问题标题】:C# - Addition on a string and replace with new stringC# - 在字符串上添加并用新字符串替换
【发布时间】:2011-07-26 20:57:48
【问题描述】:

如果我有一个从包含以下内容的文件加载的RichTextBox

TEXT MORETEXT 10.505  100.994 0  
TEXT MORETEXT -5.132  -12.994 90  
TEXT MORETEXT 100.001 -8.994  270  

和一个TextBox,其中包含用户在文本框中输入的任何内容。假设用户输入 "10.005"

我的问题是,我如何获取这个值并将其添加到包含值 10.505 的 3rd 列,- 5.132, 100.001。添加后,我想取值并替换字符串中的旧值。 所以更新后的RichTextBox 看起来像这样。

TEXT MORETEXT 20.510  100.994 0  
TEXT MORETEXT 4.873  -12.994 90  
TEXT MORETEXT 110.006 -8.994  270  

马上我可以使用以下代码从RichTextBox 中删除字符串:

    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");
            }
        });
    }

要获取 xRichTextBox 中的值,如下所示:

10.505
-5.132
100.001

yRichTextBox 看起来像:

100.994
-12.994
-8.994

但我不知道如何将它们转换为可以在其上使用 addition 的值...


编辑: 我已经搞砸了一些......我现在正在使用这段代码(如下)来尝试完成我需要它做的事情。这仅适用于 "X" (第 3 列)

但是此代码不起作用(它将用户输入连接到 xRichTextBox 的末尾,而不是在数学上将其添加到每一行......)

xDisplacementTextBox 是用户输入,xRichTextBox 是从主字符串中剥离的值。

StringBuilder stringBuilder = new StringBuilder();
string[] Lines = xRichTextBox.Text.Split('\n');
double d = double.Parse(xDisplacementTextBox.Text);

for(int i = 0; i < Lines.Length; ++i)
{
    string newThing = double.Parse((Lines[i]) + d).ToString();
    stringBuilder.AppendLine(newThing);
}

xRichTextBox.Text = stringBuilder.ToString();

这也不允许我输入带有小数的值(即 50.005)..

【问题讨论】:

标签: c# string replace add


【解决方案1】:

查看 double.Parse - 如 双 x = double.Parse(xValue);

为了扩展,为你做你的工作......

double d = double.Parse(xDisplacementTextBox.Text);
string[] Lines = xRichTextBox.Text.Split('\n');

for(int i = 0; i < Lines.Length; ++i)
{
    Match lineMatch = Regex.Match(lines[i], @"^(?<p>.*)(?<x>-?\d+\.\d+)(?<y>\s+-?\d+\.\d+\s+-?\d+\.\d+)$");
    if (lineMatch.Success)
    {
        double xValue = double.Parse(lineMatch.Groups["x"].Value) + d;
        lines[i] = lineMatch.Groups["p"] + xValue + lineMatch.Groups["p"];
    }
 }
 xRichTextBox.Text = string.Join(lines, '\n');

【讨论】:

  • 如果您愿意看一下,我已经更新了我上面的问题。
  • double.Parse((Lines[i]) + d).ToString() 想要达到什么目的?据我了解, Lines[i] 将包含类似“TEXT MORETEXT 100.001 -8.994 270”的内容,这将导致 double.Parse 引发异常。您需要将行拆分为组成部分,并单独解析数字。
  • 我试图实现将“d”添加到“Lines[i]”然后toString 将其输出回RichTextBox。对于你所说的,我会做一个 foreach 循环吗?我该怎么做?
  • 我已经扩展了我的答案 - 但我应该指出,按照其他海报的建议,以更面向对象的方式执行此操作会更好。
【解决方案2】:

字符串太多,数据结构不够。

这看起来像一个数据结构:

文本更多文本 10.505 100.994 0
文本 更多文本 -5.132 -12.994 90
文本更多文本 100.001 -8.994 270

所以,创建一个包含的类

"Text" string
"MoreText" string
10.505 - double (let's call this prop1)
100.994 - double
0 - int

我在这里推测数据值。

将你的类的列表加载到内存中。

然后,每次值更改时,将文本框值应用于您的对象列表。

伪代码:

foreach(class c in List<>)
    {
     c.prop1 = c.prop1 + (double)Textbox.value;
    }

在您的类中覆盖 ToString() 并根据需要在富文本框中显示对象。

我个人会使用列表框来显示对象。

【讨论】:

  • 我不会撒谎,这对我来说似乎很复杂(作为初学者)。有什么办法可以帮我谈谈你的想法吗?
  • 我认为你很接近。将文件读入您​​的类,操作类,然后在 GUI 上显示。现在正在读取文件并将结果直接推送到 GUI,这使得更新变得困难,因为数据结构只是一个字符串。
  • 所以我应该删除xRichTextBox.AppendText(xValue + "\n");yRichTextBox.AppendText(yValue + "\n");这行?我该如何替换它们?
  • 我在上面更新了我的问题,你愿意看看。
  • 您需要仔细定义一种将文件的每一行拆分为其组成部分的方法(例如,“TEXT MORETEXT”是一个固定长度的字符串?还是两个词?或任意数量的词,并终止按第一个数字?)。创建一个类来保存各个部分,并为其提供一个构造函数,该构造函数接受一行文本并将其拆分为多个部分(双精度和字符串)。还创建一个 ToString 方法,该方法从部件构建字符串。在阅读文件时,从每一行创建此类的项目,并将它们添加到列表中。然后你可以对零件进行简单的加法,然后重新渲染结果。
猜你喜欢
  • 2015-10-14
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 2011-10-20
  • 2022-11-17
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多