开发者

C# - Addition on a string and replace with new string

If I have a RichTextBox that is loaded from a file containg:

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

and a TextBox that contains whatever the user enters in the textbox. Let's say the user enters "10.005".

My question is, how do I take this value and add it to the 3rd column containing the values 10.505, -5.132, 100.001. Once it is added, I would like to take the value and Replace the old value in the string. SO the updated RichTextBox would look like this.

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

RIGHT NOW I am able to strip the strings from the RichTextBox by using this 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");
            }
        });
    }

To get the values in the xRichTextBox looking like:

10.505
-5.132
100.001

and the yRichTextBox looking like:

100.994
-12.994
-8.994

But I do not know how to t开发者_运维百科urn those into values that can have addition used on them...


EDIT: I have messed around with this some more... I am now using this code (below) to try to accomplish what I need it to do. This is only for the "X" (3rd column).

HOWEVER THIS CODE IS NOT WORKING (it concats the user input to the end of the xRichTextBox instead of mathematically adding it to each line..)

The xDisplacementTextBox is the user input and the xRichTextBox is the stripped values from the main string.

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();

This is also not letting me enter in values that have decimals (ie. 50.005)..


Look at double.Parse - as in double x = double.Parse(xValue);

To expand, and do your work for you...

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');


Too many strings and not enough data structures.

This looks like a data structure:

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

So, create a class that holds

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

I'm speculating on the data values here.

Load the List<> of your class into memory.

Then, apply the text box value to your list of object every time the value changes.

PsuedoCode:

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

Override ToString() in your class and display the object as needed in the rich text box.

Personally I would use a list box to display the objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜