c# -在字符串中查找数字并添加值

本文关键字:数字 添加 查找 字符串 | 更新日期: 2023-09-27 18:02:44

我有一个像这样的文件:

R.D.    P.N.      X       Y        Rot  Pkg
L5      120910    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

我有两个标签为XinputYinput的文本框。用户可以从这些文本框中输入值。一旦输入值,用户点击"GO",然后我想从文件中获取字符串,并将Xinput值添加到X列,将Yinput值添加到文件中的Y列。


我的意思是…

因此,如果用户在Xinput文本框中输入"10.552",在Yinput文本框中输入"-140.123",则新数据将看起来像这样:

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

问题:

  • 这可能吗?
  • 如果可能的话,我该怎么做呢?

c# -在字符串中查找数字并添加值

您可以使用ADO.Net将该文件作为结构化数据读取。这里有很多使用ado.net

读取文本文件的示例。

在ado数据集中获得结构化格式后,可以遍历和添加值。应该很容易。

这是一篇好文章

文章链接

试试这个,我没有你的文件,所以这是最好的猜测。我肯定我没漏掉什么。我知道这看起来有点夸张,但是通过这种方式您确实可以完全控制数据中的每个值。

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

作为第一步,为单条目引入类,如

 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);
         // ....
     }
    }

在你建立了这个结构之后,你可以很容易地添加任何值到X, Y…

逐行读取文件:从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();

将整个文件内容读入StringBuilder并尝试正则表达式