-1

I want to replace the value of two "string" members from their record in Object with the string I want.

I'm trying to use the .Contains() and replace() function on object

This is the object:

EmailParam param = new EmailParam();

And the EmailParam:

public class EmailParam {
  public string UserName {get; set;}
  public string Code {get; set;}
  public string Param1 {get; set;}
  public string Param2 {get; set;}
}

And this is the example:

string body = "My name is @UserName and my code is @Code";

param.UserName = "Tony";
param.Code = "A01";

What I want for result will be like My name is Tony and my code is A01

It is come from find body @UserName, and then replace from value Username Tony

Stfvns
  • 1,001
  • 5
  • 16
  • 42
  • 1
    Okay. The above is a specification. What's your _question_? Please fix your post so it includes a [mcve], a detailed explanation of what that code does, how that's different from what you want, and what _specifically_ you need help with. – Peter Duniho Feb 10 '21 at 04:00
  • @Stfvns the question is about doing something with string and a list (based on the title)... The sample shown is for some string and an object *that is not a list*. Consider to [edit] the question so sample and title talk about the same concept. – Alexei Levenkov Feb 10 '21 at 04:26
  • 1
    What he's saying is, we don't write code for people, we help them learn to write it themselves. You clearly haven't even attempted to do anything, so unfortunately you're in the wrong place. – Blindy Feb 10 '21 at 04:26
  • Consider reading https://stackoverflow.com/questions/733378/whats-a-good-way-of-doing-string-templating-in-net to see if "templating" is the concept you are trying to ask about. – Alexei Levenkov Feb 10 '21 at 04:28
  • It'is why I'm ask here. Because I don't have idea. How can I know to write code, when I don't have Idea? @AlexeiLevenkov – Stfvns Feb 10 '21 at 04:44
  • I cannot post an answer because this is closed and I do feel kinda sorry for you because it's obvious from your question what you want and the question shouldn't be closed. Anywho, check out this question for the answer https://stackoverflow.com/questions/33674245/c-sharp-reflection-replace-all-occurrence-of-property-with-value-in-text – Luke T O'Brien Jul 05 '22 at 09:48

2 Answers2

1

you can use this code

List<EmailParam> param = new List<EmailParam>()
{
    new EmailParam(){Code ="A01",UserName="Tony"},
    new EmailParam(){Code ="B01",UserName="William"},
};
string body = "My name is @UserName and my code is @Code";

var findItem = param.Where(a => a.UserName.Equals("Tony") && a.Code.Equals("A01")).FirstOrDefault();
body = body.Replace("@UserName", findItem.UserName).Replace("@Code", findItem.Code);
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
-1

You can use something similar to following

string body = "My name is {0} and my code is {1}"; //use placeholders instead of @UserName, @Code.
param.UserName = "Tony";
param.Code = "A01";

var convertedBody = String.Format(body, aram.UserName, param.Code);
Nantharupan
  • 594
  • 3
  • 15