public class Abbreviation
{
public string ShortName { get; set; }
public string LongName { get; set; }
}
I have a list of Abbreviation objects like this:
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")
...
I expect the result to be: "this is a test expression in a paragraph contains answer for a question"
Currently I am doing:
foreach (Abbreviation abbreviation in abbreviations)
{
test = test.Replace(abbreviation.ShortName, abbreviation.LongName);
}
result = test;
Wondering if there is a better way using a combination of Linq and Regex.
If you really wanted to shorten your code, you could use the ForEach
extension method on the List
:
abbreviations.ForEach(x=> test=test.Replace(x.ShortName, x.LongName));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With