public class Abbreviation
{
public string ShortName { get; set; }
public string LongName { get; set; }
}
我有這樣的縮寫對象的列表:字符串替換在C#中使用LINQ
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")
...
我希望得到的結果是: 「這是一款測試表達式包含答案了問題」
目前我做的:
foreach (Abbreviation abbreviation in abbreviations)
{
test = test.Replace(abbreviation.ShortName, abbreviation.LongName);
}
result = test;
不知道是否有一個更好的w ^使用Linq和Regex的組合。
LINQ是不是每一個問題的解決方案,你寫的是好的。 – Phill 2011-02-28 02:00:10