C# 无法获取我想要的字符串?

C# 无法获取我想要的字符串?,c#,C#,我正在做一个WinFormC应用程序。我有一个按钮,它将搜索一个字符串,并给出它旁边的值。这是我的密码: WebRequest Request = WebRequest.Create(Settings.Default.FoodList); WebResponse Response = Request.GetResponse(); StreamReader sr = new System.IO.StreamReader(Response.GetResponseStream()); string

我正在做一个WinFormC应用程序。我有一个按钮,它将搜索一个字符串,并给出它旁边的值。这是我的密码:

WebRequest Request = WebRequest.Create(Settings.Default.FoodList);
WebResponse Response = Request.GetResponse();
StreamReader sr = new System.IO.StreamReader(Response.GetResponseStream());

string foods = sr.ReadToEnd();
string item = TXTSearchItem.Text;

try
{
    var startIndex = foods.IndexOf(item + "-", StringComparison.CurrentCultureIgnoreCase);
    var dashIndex = foods.IndexOf("-", startIndex);
    var endIndex = foods.IndexOf("\r\n", startIndex);
    var foodName = foods.Substring(startIndex, dashIndex - startIndex);
    var footCount = foods.Substring(dashIndex + 1, endIndex - dashIndex - 1);
    MessageBox.Show($"Item {foodName} is worth {footCount} SmartPoints.", "Item search", MessageBoxButtons.OK, info);
}
catch (Exception Ex)
{
    MessageBox.Show("Item was not found.", "Item search", MessageBoxButtons.OK, warning);
}
由于某些原因,它将无法获取最后一个字符串。以下是字符串包含的内容:

   goldfish-4
   eggs-0
   banana-0
   chicken-0
   apple-0
   grapes-0

如果我输入
grapes
,它会说找不到该项目。但是如果我有一个显示食物字符串的MessageBox,它会正确地显示它。请帮忙。谢谢

也许,在这里使用
string.Split()
更容易些? 可以这样做:

string item = TXTSearchItem.Text;
string foods = sr.ReadToEnd(); // Maybe you already have ReadLines method here
var lines = foods.Split(new []{ Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
var expectedLine = lines.FirstOrDefault($"{item}-"); // assume that you have fixed format here
if (expectedLine != null)
{
   var splittedLine = expectedLine.Split('-');
   var foodName = splittedLine.First();
   var foodCount = splittedLine.Last();
   MessageBox.Show($"Item {foodName} is worth {footCount} SmartPoints.", "Item search", MessageBoxButtons.OK, info);
}
else
{
   MessageBox.Show("Item was not found.", "Item search", MessageBoxButtons.OK, warning);
}

字符串不是存储这些信息的好地方。如果这些物品的价格是唯一的(没有香蕉=1和香蕉=2)。您应该将其存储在反映这种单一性的数据结构中,这样可以简化对价格的搜索

像一个1

var输入=@“goldfish-4
蛋-0
香蕉-0
鸡-0
苹果-0
葡萄-0”;
字符串模式=@“(?\w+)-(?\d+);
RegexOptions选项=RegexOptions.Multiline;
var r=Regex.Matches(输入、模式、选项)
.Cast()
.ToDictionary(
m=>m.Groups[“key”].Value,
m=>int.Parse(m.Groups[“value”].value)
);
:RegexExplain:

那么访问数据就很容易了。甚至添加新产品或更改价格。如果您需要将其再次存储在文件中,请使用一些序列化。
1:如果价格是整数。

endIndex=foods.IndexOf(“\r\n”,startIndex)将是
-1
,因为最后一行不是以
\r\n
结尾。考虑拆分文本>代码> \r\n>代码>然后循环,这也将避免诸如“代码> BC-1 < /COD>匹配<代码> ABC-1 < /代码>之类的问题,您调试过吗?哪一行跳转到异常?谢谢你的回答。我无法在Environment.NewLine上拆分,因为它无法将字符串转换为charMy bad-从头部写入。您可以检查固定版本。
var input = @"   goldfish-4
eggs-0
banana-0
chicken-0
apple-0
grapes-0";

string pattern = @"(?<key>\w+)-(?<value>\d+)";
RegexOptions options = RegexOptions.Multiline;

var r = Regex.Matches(input, pattern, options)
            .Cast<Match>()
            .ToDictionary(
                m => m.Groups["key"].Value,
                m => int.Parse(m.Groups["value"].Value)
            );