while
循环将所有文本存储到列表中。TextBox
输入值对“X”和“Y”值使用加法。将StringBuilder
值输出到RichTextBoxes
。
private void calculateXAndYPlacement()
{s
// Reads the lines in the file to format.
var fileReader = File.OpenText(filePath + "\\Calculating X,Y File.txt");
// Creates a list for the lines to be stored in.
var fileList = new List<string>();
// Adds each line in the file to the list.
var fileLines = ""; #UPDATED @Corey Ogburn
while ((fileLines = fileReader.ReadLine()) != null) #UPDATED @Corey Ogburn
fileList.Add(fileLines); #UPDATED @Corey Ogburn
// 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 }));
// Add the results to the lists.
xResult.Add(xValue);
yResult.Add(yValue);
// Calculate the X & Y values (including the x & y displacements)
double doubleX = double.Parse(xValue);
double doubleXValue = double.Parse(xDisplacementTextBox.Text);
StringBuilder sbX = new StringBuilder();
sbX.AppendLine((doubleX + doubleXValue).ToString());
double doubleY = double.Parse(yValue);
double doubleYValue = double.Parse(yDisplacementTextBox.Text);
StringBuilder sbY = new StringBuilder();
sbY.AppendLine((doubleY + doubleYValue).ToString());
calculatedXRichTextBox.AppendText(sbX + "\n");
calculatedYRichTextBox.AppendText(sbY + "\n");
}
});
}
NOW ::: 我要做的是采用calculatedXRichTextBox
和calculatedYRichTextBox
以及替换旧值(在文件列表中)并输出它们以覆盖calculatedXRichTextBox
和calculatedYRichTextBox
。
所以,我的价值观是:
(原始档案)
TEXT TEXT 227.905 203.244 180
TEXT TEXT 242.210 181.294 180
TEXT TEXT 236.135 198.644 90
(剥离值“X”和“Y” - 在 2 不同的列表中)
227.905 203.244
242.210 181.294
236.135 198.644
(计算值将“10”添加到“X”并将“20”添加到“Y” - 将它们放入 2 不同的RichTextBoxes
) < / p>
237.905 223.244
252.210 201.294
246.135 218.644
(这就是我想要结束的内容 - 原始文件+计算值替换旧值)
TEXT TEXT 237.905 223.244 180
TEXT TEXT 252.210 201.294 180
TEXT TEXT 246.135 218.644 90
的问题: 的
答案 0 :(得分:1)
解决问题的另一种方法是将代码视为将转换应用于数据流。
这基本上就是下面的示例。它是为.NET 4.0编写的,如果你的目标是早期的运行时,你将不得不使用ReadAllLines
方法(而不是ReadLines
),而不是使用Tuple
你'我需要创建一个容器类(可以是这个类的私有)来从函数中返回多个值。
void calculateXAndYPlacement()
{
// Read data from file
var path = @"[path to your document]";
var data = File.ReadLines(path + "data.txt");
// Parse the values you'll be modifying your data by
var doubleXValue = double.Parse(xDisplacementTextBox.Text);
var doubleYValue = double.Parse(yDisplacementTextBox.Text);
// apply your transformation to all valid lines of data
var modifiedData = from line in data
where LineIsValid( line )
select ModifyLine( line, doubleXValue, doubleYValue );
// Do what you wish with the data
foreach( var dataPoint in modifiedData )
{
// grab the values from the Tuple and put them into the
// appropriate text boxes.
}
}
Tuple<string,double,double> ModifyLine(string data, double xValue, double yValue)
{
// split line into parts
var columns = Regex.Split(data, @"\s+");
columns.Dump();
// do your math on each part, I just assigned the new values
// for the sake of the example.
columns[3] = xValue.ToString();
columns[4] = yValue.ToString();
// recombine the line
return Tuple.Create( string.Join(" ", columns), xValue, yValue );
}
bool LineIsValid(string lineData)
{
return Regex.IsMatch(lineData, @"(?<x>-?\d+\.\d+)\s+(?<y>-?\d+\.\d+)");
}
答案 1 :(得分:0)
好吧,因为您已经在使用Regex来匹配您的值,为什么不尝试使用Regex.Replace来更改它们。
有关详细信息,请参阅此链接...
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=vs.71).aspx
答案 2 :(得分:0)
尝试使用string.Replace,就像在这个PSEUDO代码中一样:
int i=0;
while(newline){
if(patternMatch){
// replace old values with new ones
line.Replace(oldXList[i], newXList[i]).Replace(oldYList[i], newYList[i]);
i++;
}
}
它不是很优雅,但应该有效,对吧?