1

I have an issue here.

I'm getting string which may looks like : "simple text : {1}, and date : {3}" -> getting this from database I want to replace "{1}" with employee.Name and "{3}" with Date.Now. Which number replace with what I know from the database, there are a lot of different types so I need to do that dynamically, so String replace should look like :

String.Replace("{someNumber}", extract from the database but it is a string but should be property like emplayee.Name).

I have no idea how to do that because function put "emplayee.Name" instead of "John", which is employeeName.

Here is a sample : content = content.Replace(match.ToString(), emailVariableDictionary[ExtractIdFromMatch(match.ToString())].Variable);

emailVariableDictionary is a List which contains strings like "employee.Name, Date.Now" and like a said before String.Format put "emplyee.Name" instead of "John"

Kenik
  • 103
  • 1
  • 15

2 Answers2

0
var template = "simple text : {0}, and date : {1}";
var replacedString = string.Format(template, "John",DateTime.Now);

string format is index based if i use your "simple text : {1}, and date : {3}" you can use this as the following

var template = "simple text : {1}, and date : {3}";
var replacedString = string.Format(template,"index 0", "John","index 2",DateTime.Now);
RazorShorts
  • 115
  • 5
  • The problem is : i don't know what to put in, because i get that from data base When i have "{7}" i need to put i.e : Element.toString(), when i have {10} i need to put "Template.toString()". I want to "Template.toString() to be treated like a method or value from object proeprty. – Kenik Jul 12 '18 at 12:53
0

Please use String Interpolation Concept. Its available in below link

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated

Example:

string name = "Mark";
var date = DateTime.Now;

// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Both calls produce the same output that is similar to:
// Hello, Mark! Today is Wednesday, it's 19:40 now.
Antoine
  • 352
  • 1
  • 10
  • 18
jamir
  • 62
  • 1
  • 8