bit500
bit500

Reputation: 27

Replace date in string

I have email subjects stored in DB like this "Sample Result {MM/dd/yyyy}" or "Sample {MM/dd/yyyy} Result". I need to get the string and replace the date format with current date. The date formats in the string might vary for example it can be "{dd-MM-yyyy}" also. If the data in DB was like "Sample Result {0:MM/dd/yyyy}" I can use the below code:

string.Format(formattedSubject, DateTime.Now)

But how to make it work with "Sample Result {MM/dd/yyyy}" I know I can use:

string formattedSubject = subject.Replace("{","{0:");

But I think that's not the ideal solution. Any other ideas?

Upvotes: 0

Views: 1819

Answers (3)

ChrisBD
ChrisBD

Reputation: 9209

Dates and times are always problematic, especially when taking different cultures and time zones into account.

As you specify the date format in the email header could you change it to to store the culture information instead (please forgive the messy code) e.g.

    DateTime date = new DateTime(2021, 10, 31, 17, 4, 32);
    
    string emailHeader = "Sample Result {en-US}";
    
    string cultureString = emailHeader.Substring(emailHeader.IndexOf("{")+1,emailHeader.IndexOf("}")-emailHeader.IndexOf("{")-1);
    CultureInfo culture = new CultureInfo(cultureString);
    
    emailHeader = emailHeader.Replace(cultureString, date.ToString("d", culture));
    Console.WriteLine($"{culture} email header = \"{emailHeader}\"");


    emailHeader = "Sample Result {en-GB}";
    cultureString = emailHeader.Substring(emailHeader.IndexOf("{")+1,emailHeader.IndexOf("}")-emailHeader.IndexOf("{")-1);
    culture = new CultureInfo(cultureString);

    emailHeader = emailHeader.Replace(cultureString, date.ToString("d", culture));
    Console.WriteLine($"{culture} email header = \"{emailHeader}\"");

    

Gives ouput:

en-US email header = "Sample Result {10/31/2021}"
en-GB email header = "Sample Result {31/10/2021}"

Upvotes: 0

Alberto
Alberto

Reputation: 15941

Assuming the date format is always compatible with the patterns accepted by the DateTime.ToString method, you can use a regex to extract the format and do the replace:

string s = "Sample Result {MM/dd/yyyy}";
Regex r = new Regex(@"\{(.*?)\}");
string dateFormat = r.Match(s).Groups[1].Value; //Get the first matching group
string formatted = r.Replace(s,DateTime.Now.ToString(dateFormat));

obviously you should add all the needed null-checks and error handling.

Upvotes: 1

DSMTurboAWD
DSMTurboAWD

Reputation: 365

Not super ideal due to the variability of the formatting, but if it is always between {} it could work in the following way

Related:

How to get a string between two characters?

and

How to replace the text between two characters in c#

// This is using the two above answers to grab the string between the {}
// and replace with a new date
string originalString = "Sample Result {MM/dd/yyyy}"
string s = originalString;

s = s.substring(s.indexOf("{") + 1);
s = s.substring(0, s.indexOf("}"));

s = DateTime.Now;

Regex regexGoodness = new Regex(@"\{([^\}]+)\}");
originalString = regexGoodness.Replace(originalString, s);

Upvotes: 0

Related Questions