下面的文本文件名为urlmap.txt in App_Data
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中的每个字符串都是唯一的,因此不存在复制的可能性。
我该怎么做?
到目前为止,这就是我所拥有的,但我无法决定如何在它面前收回这段文字。
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的内容,所以我还需要一种检索记录的优化方法。
发布于 2015-07-09 16:43:21
只需使用拆分:
if (line.Contains(mySearchString))
{
var text = line.Split(",")[1];
/*do something else with text*/
}https://stackoverflow.com/questions/31323493
复制相似问题