-2

I have a Dictionary<string,dynamic> of key value pairs.

I also have a string script in which I need to replace all occurrences of the key with corresponding value from the dictionary.

Eg : Contents of Dictionary :

Param1 : true
Param2 : "False"
Param3 : 123
Param4 : "1234"

String script = " I have Param1 and Param2 and Param3 and Param4 ".

Now I wish to convert it to

script = " I have true and "False" and 123 and "1234" "

How can I achieve this ? I have tried script.Replace() but it doesn't work for datatypes other than string and if I use ToString() for others , its capitalizing the Boolean values.

Edit : I also went through this link Why does Boolean.ToString output "True" and not "true".

  • `script = " I have true and "False" and 123 and "1234" "` is not valid C#. A string cannot contain non-string values. – Dai Jul 19 '18 at 05:33
  • Actually , I need the output for a JSON Object so that's not an issue. – Varad Bhatnagar Jul 19 '18 at 05:34
  • 1
    Can I ask why case matters only for param1? the logical step would be to just call `ToLower` – Sayse Jul 19 '18 at 05:34
  • Lets say that Param1 is a `Boolean` value and Param2 and Param4 are `string` and Param3 is an `int`. `ToLower` is fine, but its a `string` function and I will get extra quotes when I use it on Param1. – Varad Bhatnagar Jul 19 '18 at 05:38
  • `ToLower` does not create quotes, you should create an [mcve] – Sayse Jul 19 '18 at 05:39
  • Why `Dictionary` and not `Dictionary` or `Dictionary` or even `Dictionary>`? – Enigmativity Jul 19 '18 at 05:42
  • 1
    You've said *"I need the output for a JSON Object"*, so this is an XY problem and you need to show us the wider context, because to quote from the question you linked... "ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for **display**.". Object.ToString is not a **serialization** mechanism. – Richardissimo Jul 19 '18 at 05:43
  • @Enigmativity If it is `Dictionary ` , I will not be able to differentiate between a `string "true"` and a `bool true` , as both will have same representation i.e. `"true"`. – Varad Bhatnagar Jul 19 '18 at 05:44
  • @VaradBhatnagar - No, they wouldn't. `new Dictionary() { { "Param1", "true" }, { "Param2", "\"False\"" }, };` – Enigmativity Jul 19 '18 at 05:50
  • @VaradBhatnagar `Boolean.ToString()` returns result capitalized, because if you look at source code of `Boolean.ToString()`, it returns `return TrueLiteral;` if its value is `true` and `TrueLiteral` is declared like this: `internal const String TrueLiteral = "True";`. **[boolean.cs](https://referencesource.microsoft.com/#mscorlib/system/boolean.cs,f1b135ff6c380b37)**. – SᴇM Jul 19 '18 at 05:55

3 Answers3

0

Give this a go:

var map = new Dictionary<string, object>()
{
    { "Param1", true },
    { "Param2", "False" },
    { "Param3", 123 },
    { "Param4", "1234" },
};

var script = " I have Param1 and Param2 and Param3 and Param4 ";

var output = map.Aggregate(script,
    (s, kvp) => s.Replace(kvp.Key, kvp.Value is string ? $"\"{kvp.Value}\"" : kvp.Value.ToString()));

That gives the equivalent of:

var output = " I have True and \"False\" and 123 and \"1234\" ";

You may just need a .ToLower() in there.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

With RegularExpressions using the pattern "Param\\d+" you can use String.Relace() with the found matches. You would have to check if the value from your map is of type bool so you can String.ToLower() for your desired result.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        var map = new Dictionary<string, object>()
        {
            { "Param1", true },
            { "Param2", "\"False\"" },
            { "Param3", 123 },
            { "Param4", "\"1234\"" },
        };

        var script = " I have Param1 and Param2 and Param3 and Param4 ";
        MatchCollection matches = Regex.Matches(script, "Param\\d+");
        foreach (Match match in matches)
        {
            object val = map[match.Value];
            script = script.Replace(match.Value, val is bool ? val.ToString().ToLower() : val.ToString());
        }

        Console.WriteLine(script);
    }
}

Result:

 I have true and "False" and 123 and "1234"

Fiddle Demo

Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

You can do it in following way.

Boolean x = false;
Dictionary<string, dynamic> k = new Dictionary<string, dynamic>();
k.Add("Param1", x.ToString().ToLower());
k.Add("Param2", 123);
Console.WriteLine(string.Format("hi {0} -- {1}", k.Values.ToArray()));