【问题标题】:How to get data from text table string in C# more reliably and quickly如何更可靠、更快速地从 C# 中的文本表字符串中获取数据
【发布时间】:2017-11-16 08:05:31
【问题描述】:

我正在将一些数据从 SAP GUI 屏幕复制到剪贴板。当我将它粘贴到记事本或任何文本编辑器上时,它如下所示。

我只想要剪贴板文本表字符串中的 3 个字段[创建者、采购订单日期、文档]。

目前我正在设法读取如下数据。

public class DocData
{
    public string CreatedBy { get; set; }
    public string PODate { get; set; }
    public string Document { get; set; }
}

private void GetDocumentData()
{
  var clipboardData = Clipboard.GetText(TextDataFormat.Text);
  List<DocData> docDataList = new List<DocData>();
  for (int separatorCounter = 20; separatorCounter < clipboardData.Count(); separatorCounter = separatorCounter + 13)
  {
   DocData docData = new DocData();
   int index = GetNthIndex(clipboardData, '|', separatorCounter);
   if (index != -1)
   {
    var dataString = clipboardData.Substring(index + 1);
    var sepIndex = dataString.IndexOf('|');
    if (sepIndex != -1)
    {
     docData.CreatedBy = dataString.Substring(0, sepIndex).Trim();
    }
   }
   index = GetNthIndex(clipboardData, '|', separatorCounter + 2);
   if (index != -1)
   {
    var dataString = clipboardData.Substring(index + 1);
    var sepIndex = dataString.IndexOf('|');
    if (sepIndex != -1)
    {
     docData.PODate = dataString.Substring(0, sepIndex).Trim();
    }
   }
   index = GetNthIndex(clipboardData, '|', separatorCounter + 4);
   if (index != -1)
   {
    var dataString = clipboardData.Substring(index + 1);
    var sepIndex = dataString.IndexOf('|');
    if (sepIndex != -1)
    {
     docData.Document = dataString.Substring(0, sepIndex).Trim();
    }
   }
   if (!string.IsNullOrEmpty(docData.Document))
   {
    docDataList.Add(docData);
   }
  }
 }

任何人都可以提出更好的方法来做到这一点而无需过多地操作字符串。

【问题讨论】:

  • Split怎么样
  • 用子程序代替复制/粘贴怎么样?
  • String.Split(将为您提供每一行的数组)和 .Trim(清除各个字段中的空白)应该可以很好地为您完成大部分繁重的工作!
  • 更清楚一点:使用 CR 或 CRLF 作为分隔符(取决于您的文件行终止符)的剪贴板文本上的 String.Split 将为您提供单独的行,然后在这些行上使用 String.Split “|” (这将为您提供每一行的数组)和 .Trim(清除各个字段中的空白)。
  • @MohitShrivastava Split 完美。非常感谢您的指导。

标签: c# string text clipboard


【解决方案1】:

这是解决您的问题的一种方法。此示例首先按行拆分文本,然后使用 header 查找所需数据的位置,然后遍历行并获取数据。

尚未对其进行测试(如果您粘贴文本而不是该屏幕截图,我可以 :)),但它应该可以工作。查看代码中的 cmets 进行解释。

如果您还有其他问题,请随时提出。

编辑:添加缺少的方法

//result list
List<DocData> docDataList = new List<DocData>();

//get clipboard data
string clipboardData = Clipboard.GetText(TextDataFormat.Text);

//split it to array of lines using Environment.NewLine (\r\n);
string[] reportLines = clipboardData.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

if (reportLines.Length < 4) //just an example of checking if there's enough lines
    throw new Exception("wrong number of lines");

//get correct indexes by reading header (positioned on third line, index 2) - in case order changes
int createdIndex = GetColumnIndex(reportLines[2], "Created by");
int PODateIndex = GetColumnIndex(reportLines[2], "PO Date");
int documentIndex = GetColumnIndex(reportLines[2], "Document");

//when you have indexes, loop through remaining lines, starting at fifth (index 4) and get data from that "columns"
for (int i = 4; i<reportLines.Length; i++)
{
    //now split current line by pipes
    string[] lineData = reportLines[i].Split('|');
    //create instance of your class and add data from specific indexes
    DocData docData = new DocData()
    {
        CreatedBy = lineData[createdIndex].Trim(), //also, trim ending spaces,
        PODate = lineData[PODateIndex].Trim(),
        Document = lineData[documentIndex].Trim()
    };
    docDataList.Add(docData);
}

public int GetColumnIndex(string headerLine, string columnName)
{
    List<string> headerNames = headerLine.Split('|').ToList(); //split header columns using pipe |.

    //get index of column by trimming and searching throught header column names
    return headerNames.IndexOf(headerNames.FirstOrDefault(h => h.Trim().Equals(columnName, StringComparison.InvariantCultureIgnoreCase)));

}

【讨论】:

  • 很有帮助。非常感谢您的宝贵时间。
  • @a_programmer 很高兴我能提供帮助。我看到没有人在写答案(尽管 cmets 很好),我不知道你的知识水平所以我花了一点时间写一个例子
  • 感谢您对人民的理解程度。我不是专家,但我想成为。我自己用'string.Split()'获取数据,你的代码也指导我让我的代码完美和工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
  • 2018-04-16
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
相关资源
最近更新 更多