C# - Find Number in String and Add a Value
I have a file that looks like this:
R.D. P.N. X Y Rot Pkg
L5 1209开发者_运维知识库10 64.770 98.425 180 SOP8
L4 120911 -69.850 98.425 180 SOIC12
L10 120911 -19.685 83.820 180 SOIC10
L9 120911 25.400 83.820 180 0603
L5 120910 62.484 98.425 180 SOP8
And I have two text boxes labled Xinput
and Yinput
. From these text boxes the user enters values into them. Once the values are entered and the user clicks "GO" then I would like to take the string from the file and add the Xinput
value to the X
column and the Yinput
value to the Y
column from the file.
WHAT I MEAN...
So if the user inputs "10.552" into the Xinput
textbox and "-140.123" into the Yinput
textbox, the new data would look like this:
R.D. P.N. X Y Rot Pkg
L5 120910 75.322 -41.698 180 SOP8
L4 120911 -59.298 -41.698 180 SOIC12
L10 120911 -9.133 -56.303 180 SOIC10
L9 120911 35.952 -56.303 180 0603
L5 120910 73.036 -41.698 180 SOP8
QUESTIONS:
- Is this possible to do?
- How would I go about doing this if it is possible?
You can read the file as structured data using ADO.Net. There are ample samples for reading text files with ado.net
Once you get it in structured format in ado dataset, you can traverse and add values. Should be fairly easy.
Here is a good article
Article Link
Try this out, I don't have your file so this is a best guess. I'm sure I didn't leave out much. I know it seems overkill, but you really have full control over each value in your data this way.
public class ComponentLocation
{
public string ReferenceDesignator { get; set; }
public string PartNumber { get; set; }
public double xValue { get; set; }
public double yValue { get; set; }
public int Rotation { get; set; }
public string Package { get; set; }
}
public IEnumerable<ComponentLocation> ParseColumns(string fileName)
{
IEnumerable<string> rawData = File.ReadLines(fileName);
var rows = rawData.Skip(1).Select(l => l.Split('\t')).Select(str => new ComponentLocation
{
ReferenceDesignator = str[0],
PartNumber = str[1],
xValue = double.Parse(str[2]),
yValue = double.Parse(str[3]),
Rotation = int.Parse(str[4]),
Package = str[5]
});
return rows.ToList();
}
public void DoWork(double x, double y, string fileName)
{
var components = ParseColumns(fileName);
//Add values
foreach (var component in components)
{
component.xValue += x;
component.yValue += y;
}
//build file
StringBuilder sbData = new StringBuilder();
//header
sbData.AppendLine("R.D.\tP.N.\tX\tY\tRot\tPkg");
//data
foreach (var component in components)
{
sbData.Append(component.ReferenceDesignator).Append('\t');
sbData.Append(component.PartNumber).Append('\t');
sbData.AppendFormat("{0:###.###}", component.xValue).Append('\t');
sbData.AppendFormat("{0:###.###}", component.yValue).Append('\t');
sbData.Append(component.Rotation).Append('\t');
sbData.Append(component.Package).Append('\t').AppendLine();
}
//write
File.WriteAllText(fileName, sbData.ToString());
}
//call DoWork
DoWork(10.552, -140.123, @"C:\myfile.txt")
As first step introduce class for single entry, smth obvious like
class Entry
{
public string Rd { get; private set; }
public string Pn { get; private set; }
public double X { get; set; }
// ... declare other properties
// Initializes a new instance of the Entry class
public Entry(string rawData)
{
if (rawData == null)
{
throw new ArgumentNullException("rawData", "Nullable data structure can not be processed");
}
string[] values = rawData.Split('\t');
if (values == null || values.Count() != 6)
{
throw new ArgumentException("rawData", "Invalid data structure can not be processed");
}
this.Rd = values[0];
Double.TryParse(values[2], out this.X);
// ....
}
}
After you have build this structure you can easily add any value to X, Y...
To read a File line by line: From MSDN
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
Read the entire file contents into a StringBuilder
and try regular expression.
精彩评论