【问题标题】:How to get index of string found in list and equal to input string如何获取在列表中找到并等于输入字符串的字符串索引
【发布时间】:2017-05-12 07:45:22
【问题描述】:

我正在尝试从列表中获取已创建字符串的索引号。我有 500 个字符串数值的列表:

 List<string> List = new List<string>();

所以我如果有一些输入字符串等于我的列表中的某个现有字符串,例如:

  string numbStr = "54";

现在我想在列表中找到相等字符串的索引,字符串“54”的索引应该是索引 53。所以如果我的输入字符串值小于 101,我有正确的结果。如果输入值为字符串“54”,我在列表中找到相等的字符串“54”,则索引值为53,+1我得到了所需的索引,这样:

    int index = List.FindIndex(x => x.StartsWith(numbStr));

但是,如果我的输入数字大于 101,例如 string numbStr = "308"; 索引结果是 517,“500”那一行实际上是 499 我得到了 901 等等。检查和计数的列表中的所有字符串都等于其索引按顺序排列用 shift -1 与字符串名称进行比较。

所以不确定这个错误结果的原因是什么,需要建议才能弄清楚。

列表看起来像这样,可能是因为我不确定空格:

123 b4 c1 nnn
124 b4 c1 nnn
125 b4 c1 nnn
126 b4 c1 nnn

【问题讨论】:

  • 尝试使用“Equals”方法或==而不是“StartsWith”,看看它是否有效。这是因为,Starts with 可能导致任何顺序的多个结果,因此索引可能不准确
  • 你是如何填充输入列表的?该列表是否已排序?
  • @Abhilash R Vankayala 你好,就像下面的答案一样,我这样得到 0
  • @un-lucky 你好,在循环中一一列出加载字符串,相当于显示或打印到文本文件中,一切都按顺序排列。当列表完成时,加载发生在此条件之前
  • 如果你的List&lt;string&gt;只有500个值,你的代码怎么能返回517或901之类的值呢?它应该返回的最大值是 500(即 499 + 1)。

标签: c# string linq list indexing


【解决方案1】:

例如,如果您将12 作为输入传递,则下面的行可能会导致您使用StartsWith 匹配12123 的问题。

int index = List.FindIndex(x => x.StartsWith(numbStr));

解决方案:

像下面这样更改那行代码并试一试

int index = List.FindIndex(x => x.Split(' ')[0] ==numbStr);

或者干脆

int index = List.FindIndex(x => x.StartsWith(numbStr+" "));

更新:

只是为了确保您的物品井井有条,您可以执行以下操作..

int index = List.OrderBy(x=>Convert.ToInt32(x.Split(' ')[0])).ToList()
                .FindIndex(x => x.Split(' ')[0] ==numbStr);

【讨论】:

  • 你好,这样我得到了相同的结果 0 就好像使用 Equals 而不是 StartsWith
  • @lado :你确定你的字符串中没有空格吗?将示例数组数据添加到问题中。
  • @DarkKnight 是的,有一个空格。问题已编辑,无论如何不知道如何进一步
  • with StartsWith if input "500" 结果是 901 与我的问题中显示的相同,Split 再次为 0,并且您上次更新 OrderBy 我得到了 IOrderedEnumareable 和 @987654333 @找不到扩展方法
  • @lado:再次检查update 选择。已添加ToList
【解决方案2】:

变量List 应该是list。如果是属性或方法,则仅以大写开头。

虽然我觉得你的情况由于格式的原因很难理解,但我还是推导出了这三个假设:

  1. 将作为字符串提供的数字,可以始终在列表中找到。

  2. 您的列表是有序的,它应该始终包含相应索引处的数字减 1(list[0] == -1、list[1] == 0 等)。

  3. 目标是检查提供的数字字符串是否存在于列表的正确位置(如果愿意,可以对列表进行单元测试)。

按照这些假设,我会说解析字符串并检查指定的位置。这意味着您不必为每个提供的数字搜索整个列表(因此速度要快得多)。

var list = new List<string>() { '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' };
string test1s = "1";
string test2s = "2";
string test3s = "10";
string test4s = "11";

int test1i = null;
int test2i = null;
int test3i = null;
int test4i = null;

Int.TryParse(test1s, test1i);
Int.TryParse(test2s, test2i);
Int.TryParse(test3s, test3i);
Int.TryParse(test4s, test4i);

/* in case you do not want to unit test, 
* you can obviously do something else with the variable here.
* check on list size is to prevent index out of range exceptions and is optional */
Assert.IsTrue(test1i.HasValue && list.Count() > test1i && list[test1i-1] == test1s));
Assert.IsTrue(test2.HasValue && list.Count() > test2i && list[test2i-1] == test1s));
Assert.IsTrue(test3.HasValue && list.Count() > test3i && list[test3i-1] == test1s));
Assert.IsFalse(test4.HasValue && list.Count() > test4i && list[test4i-1] == test1s));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2021-12-18
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 2015-07-01
    • 2019-04-29
    相关资源
    最近更新 更多