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;
와트 더 나은이 있다면 궁금 Linq와 Regex의 조합을 사용합니다. 당신이 정말로 당신의 코드를 단축하고 싶다면
LINQ 아래 하나가 모든 문제에 대한 해결책이 아니다 시도 , 당신이 쓴 것은 괜찮습니다. – Phill