【问题标题】:Find string in list by index of found string通过找到的字符串的索引在列表中查找字符串
【发布时间】:2017-01-05 07:13:00
【问题描述】:

如果我发现列表中存在相等字符串的字符串索引:

int index = myList.FindIndex(x => x.StartsWith(inputStr));

什么是接近字符串 +1 -1 并将其替换为某个值的正确且快速的方法:

如此理想的结果,如果我的列表内容是:

0. hello world 1
1. hello world 2
2. hello world 3
3. hello world 4

并且输入字符串等于“hello world 2”,我希望通过索引 +1 “hello world 3”或 -1 “hello world 1”找到 +1 -1 索引并将其替换为“X”或只是将字符串作为变量用于其他用途。

0. hello world 1
1. hello world 2
2. X
3. hello world 4

【问题讨论】:

  • 请注意,line => line == input 表示您搜索等于input 的行,而使用x => x.StartsWith(inputStr) 您只检查该行是否以input 字符串开头。
  • 您在这里面临的困难是什么?

标签: c# string list indexing


【解决方案1】:

如果我正确理解您的问题,您可以像这样直接访问它们:

int index = myList.FindIndex(x => x.StartsWith(inputStr));

string previous = myList[index - 1];
string next = myList[index + 1];

// you can change them like this

myList[index - 1] = "x";
myList[index + 1] = "x";

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 2019-04-29
    • 2021-11-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多