首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从逗号分隔的文件中读取文本

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

Stack Overflow用户
提问于 2015-07-09 16:39:36
回答 1查看 64关注 0票数 0

下面的文本文件名为urlmap.txt in App_Data

代码语言:javascript
复制
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“这样的字符串,并搜索并检索它前面的文本,即”运动会/1140/microsoft-2键-鼠标带垫“。

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

urlmap.txt中的每个字符串都是唯一的,因此不存在复制的可能性。

我该怎么做?

到目前为止,这就是我所拥有的,但我无法决定如何在它面前收回这段文字。

代码语言:javascript
复制
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+lines的内容,所以我还需要一种检索记录的优化方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-09 16:43:21

只需使用拆分:

代码语言:javascript
复制
if (line.Contains(mySearchString)) 
{
   var text = line.Split(",")[1];
   /*do something else with text*/
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31323493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档