C# - 在字符串上添加并替换为新字符串

时间:2011-07-26 20:57:48

标签: c# string replace add

如果我有一个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 。一旦添加,我想取值并替换字符串中的旧值。 SO 更新的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

但我不知道如何将这些转化为可以在其上使用添加的值...


修改 的 我已经搞砸了更多...我现在使用这个代码(下面)来尝试完成我需要它做的事情。这仅适用于 “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)..

2 个答案:

答案 0 :(得分:4)

看看double.Parse - 就像在     double 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');

答案 1 :(得分:3)

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

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

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

所以,创建一个包含

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

我在这里推测数据值。

加载列表&lt;&gt;你的班级进入记忆。

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

伪码:

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

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

就个人而言,我会使用一个列表框来显示对象。

相关问题