繁体   English   中英

从逗号分隔的文件中读取文本

[英]Reading Text from Comma-Delimited file

我在App_Data中有以下名为urlmap.txt的文本文件

ShowProduct.aspx?ID=1143, laptops/1143/laptops-for-beginner-experienced-engineers
ShowProduct.aspx?ID=1142, desktops/1142/dynamic-difference-desktops
ShowProduct.aspx?ID=1141, keyboards/1141/bluetooth-ready-keyboards
ShowProduct.aspx?ID=1140, mouse/1140/microsoft-2key-mouse-with-pad
ShowProduct.aspx?ID=1139, mouse/1139/logitech-3key-mouse-auto-shutoff
and about 2000 such entries....

我想传递“ ShowProduct.aspx?ID = 1140”之类的字符串,并搜索并检索其前面的文本,即“ mouse / 1140 / microsoft-2key-mouse-with-pad”

如果找不到此字符串,则不会检索任何内容。

urlmap.txt中的每个字符串都是唯一的,因此没有重复的机会

我怎样才能做到这一点?

这是我到目前为止的内容,但是我无法确定如何重新显示前面的文字

string line;
StringBuilder sb = new StringBuilder();

using (System.IO.StreamReader file = new System.IO.StreamReader("App_Data\urlmap.txt"))
{
    while ((line = file.ReadLine()) != null)
    {
          if (line.Contains(mySearchString)) 
          {

          }
    }
}

由于文本文件包含大约2000行,因此我还需要一种优化的方式来检索记录。

只需使用拆分:

if (line.Contains(mySearchString)) 
{
   var text = line.Split(",")[1];
   /*do something else with text*/
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM