【发布时间】:2018-11-09 15:58:56
【问题描述】:
我有一个缩写和位置列表,我想替换特定的特定缩写而不是所有缩写。
public class Abbreviation
{
public int Pos { get; set; }
public string ShortName { get; set; }
public string LongName { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Abbreviation> abbreviations = new List<Abbreviation>();
abbreviations.Add(new Abbreviation() { Pos = 28, ShortName = "exp.", LongName = "expression" });
abbreviations.Add(new Abbreviation() { Pos = 38, ShortName = "para.", LongName = "paragraph" });
abbreviations.Add(new Abbreviation() { Pos = 51, ShortName = "ans.", LongName = "answer" });
string test = "What is exp.? This is a test exp. in a para. contains ans. for a question";
abbreviations.ForEach(x => ...); // Need a LINQ expression
Console.WriteLine(test);
}
}
如何在 LINQ 中做到这一点?
【问题讨论】:
-
那么
Pos对应字符串中的位置?这意味着如果exp.不在第 30 位,它应该被忽略(即不被替换)? -
是的,没错
-
你当前的代码有效吗?
-
是第一个“exp”。第 8 位?
-
你应该提供一个清晰的列表,你已经尝试过什么,以及一个清晰的输入、输出。我建议扩展您的问题,以使人们更容易理解这个问题。仅供参考。通常,问题越漂亮,答案就越漂亮。一半的问题=一半的答案。