1

In my program, there is a string like this: Lorem ipsum dolor sit amet, {CONSECTETUR} adipiscing elit. Cras mattis lacinia ex, non {GRAVIDA} quam malesuada in. Proin pretium tellus eu posuere ullamcorper. Suspendisse {SAGITTIS} ullamcorper pulvinar. Nam pharetra mi ut risus ornare dictum. Proin tortor nibh, euismod ac viverra sit amet, {SUSCIPIT} eget magna. Proin et feugiat neque, nec pellentesque magna. Duis {SOLLICITUDIN} ligula ac augue viverra, ac dignissim ipsum elementum. Nulla facilisi. Duis lacinia nec nisl sed ultrices. Morbi {VESTIBULUM} tortor vitae dui elementum pellentesque. Morbi egestas ultricies cursus. Pellentesque euismod consequat leo. Cras vitae sem non leo volutpat egestas. Morbi in leo eget sem lacinia finibus.

And a JSON file like this:

[
  {
    "Key": "{CONSECTETUR}",
    "Value": "17/7/2018"
  },
  {
    "Key": "{GRAVIDA}",
    "Value": "21/7/2018"
  },
  {
    "Key": "{SAGITTIS}",
    "Value": "5/2/2020"
  },
  {
    "Key": "{SUSCIPIT}",
    "Value": "26/3/2020"
  },
  {
    "Key": "{SOLLICITUDIN}",
    "Value": "some value with lowecase"
  },
  {
    "Key": "{VESTIBULUM}",
    "Value": "6 years 3 months 17 days"
  }
]

I want to replace keys with values. For this I can get keys and values with Newtonsoft Json.Net like this:

public class Info
{
    public string Key { get; set; }
    public string Value { get; set; }
}

var ls = JsonConvert.DeserializeObject<List<Info>>(PATH + "info.json"));

But I don't know how to complete the rest and how to replace the values in the json file with the keys in the string.

Habip Oğuz
  • 921
  • 5
  • 17
  • Are you looking for [Replace Multiple String Elements in C#](https://stackoverflow.com/q/1321331/3744182)? – dbc May 07 '20 at 14:44

1 Answers1

2
string text = "Lorem ipsum...";
var ls = JsonConvert.DeserializeObject<List<Info>>(PATH + "info.json"));

foreach (var item in ls)
{
    text = text.Replace(item.Key, item.Value);
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Omer Faruk KAYA
  • 342
  • 2
  • 10