1

I have taken over the redevelopment of an app which needs to come over from Xamarin Forms to .NET MAUI and while I move it over, I'm taking the opportinuity to fix and improve the code base. Makes sense to do it now that later.

In the code, I've seen a lot of these sort of chunks of code and want to improve them

if (openClains.Description.Contains("January"))
 {
       openClains.Description = openClains.Description.Replace("January", Translate.CheckBrand(Languages.Resource.January));
 }
 else if (openClains.Description.Contains("February"))
 {
        openClains.Description = openClains.Description.Replace("February", Translate.CheckBrand(Languages.Resource.Febuary));
 }
 else if (openClains.Description.Contains("March"))
 {
        openClains.Description = openClains.Description.Replace("March", Translate.CheckBrand(Languages.Resource.March));
 } (and so on)

It doesn't look nice and seems overly complex when all it does is look for a month name (from returned from a webservice) and replace it with the month name in a different language.

I did consider writing an extension method, but the issue is the selection of the name from the resources file based on the name fed in plus it doesn't really remove the if/else

Is there a simpler way using (say) LINQ to do the replace rather than a pile of if/else or switch/case conditions?

Nodoid
  • 1,449
  • 3
  • 24
  • 42
  • 3
    why Linq? a Dictionary seems perfect – Mario Vernari Apr 18 '23 at 09:52
  • 1
    Create an array or months and then use a for loop to enumerate through the array. TLanguages.Resource.January is probably an enumeration that uses for the months a number 1 to 12. – jdweng Apr 18 '23 at 09:52
  • 3
    Wow this looks ripe for some weird replacements. "May" is quite a common word in English... "May need some work in January" – Matthew Watson Apr 18 '23 at 09:55
  • @MarioVernari because why write the loop yourself if LINQ can do it for you? – Good Night Nerd Pride Apr 18 '23 at 10:10
  • 1
    There is no need to test with an if before each replace – Jason Apr 18 '23 at 10:32
  • Ok, guys. I understand the question asks for string replacement. But the problem he describes has other solutions. That are much better than modifying the strings themselves. I have done something very similar, for very old API (that could not be rewritten) for android app, modifying the parsing at the transport level. Why is this question locked so fast? Sometimes the person who asks has some ideas, that are not always the best solution. Lock it based on the problem, not the wrong idea of solution. – H.A.H. Apr 18 '23 at 12:41

1 Answers1

0

Here's one way to go about it:

var replacements = new[] {
    (from: "January", to: "JAN"),
    (from: "February", to: "FEB"),
    // ...
};

var descr = "Open from January to February!";
descr = replacements.Aggregate(
    descr,
    (s, repl) => s.Replace(repl.from, repl.to));

Debug.Assert(descr == "Open from JAN to FEB!");

A safer, but slower option is to use a regular expression that only matches the from value if it's a word on its own. E.g. match "May" in "May is...", but not in "Maybe not...".

descr = replacements.Aggregate(
    descr,
    (s, repl) => Regex.Replace(s, @$"\b{repl.from}\b", repl.to));
Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65