【发布时间】:2011-07-05 11:35:29
【问题描述】:
public class Abbreviation
{
public string ShortName { get; set; }
public string LongName { get; set; }
}
我有一个 Abbreviation 对象列表,如下所示:
List abbreviations = new List();
abbreviations.add(new Abbreviation() {ShortName = "exp.", LongName = "expression"});
abbreviations.add(new Abbreviation() {ShortName = "para.", LongName = "paragraph"});
abbreviations.add(new Abbreviation() {ShortName = "ans.", LongName = "answer"});
string test = "this is a test exp. in a para. contains ans. for a question";
string result = test.Replace("exp.", "expression")
...
我希望结果是: "this is a test expression in a paragraph contains answer for a question"
目前我在做:
foreach (Abbreviation abbreviation in abbreviations)
{
test = test.Replace(abbreviation.ShortName, abbreviation.LongName);
}
result = test;
想知道是否有更好的方法结合使用 Linq 和 Regex。
【问题讨论】:
-
LINQ 不能解决所有问题,你写的很好。