I have a Windows UWP application in C# and .NetCore. I have a special type of printer that requires me to send specific strings to print a label. Therefore, I am storing my label definition/format in a separate text file. When the app loads and tries to print, I then read the text file and load in the string. However, there are some variables inside of this string when I read it in that I need to replace with actual data. For the time being, I am using placeholders and highlighting them with curly braces. However, I could use parens or something else if that is easier. Also, I know that with string.Format, you would typically enumerate your variables to be replace in the curly braces with numbers but I chose to use words to make the definition text file more readable.
So, here is the raw string once I grab it from the text file and parse it (and store it in my class property).
^XA ^LL300 ^FO125,575 ^A0R,30,30 ^FDRecipient: {Recipient} ^FS ^FO175,575 ^A0R,30,30 ^FDM/S: {EmpMail} ^FS ^FO275,575 ^A0R,30,30 ^FDTelephone: {EmpPhone} ^FS ^FO350,575 ^A0R,30,30 ^FDProcessed: {DateTime.Now} ^FS ^FO290,210 ^B7R,8,5,7,21,N ^FD{EmpNum} ^FS ^XZ
In this, you can see the five or so variable placeholders, in the curly braces, that I want to replace with real data.
The question is, how do I take a string that is now stored in a property within my class and replace all of the placeholders with data.
To give some context, here is what I have so far. I don't get any exceptions with this but it is not actually replacing the data like it should. For example, the code sample below should replace {DateTime.Now} with the actual DateTime.Now but it does not, I still see my placeholder.
Here are the two fields needed for this code
public const string LabelDateTime = "{DateTime.Now}"; private string locationLabel = string.Empty;
locationLabel is populated during the constructor of the class, when I read in the text file with the string that I show above.
Here is the method where I will do the actual detection and replacement of all the placeholders. For now, as a test, I am only doing DateTime.Now.
private string replacePrintStringVariablesWithValues() { var finalPrintString = this.labelString; //Below we will check for and replace all of the possible variables to the print string. //TODO: we will need to refine/add more to this as labels are created. I tried to capture most now. if (labelString.Contains(LabelDateTime)) { finalPrintString = ReplaceWholeWord(finalPrintString, LabelDateTime, DateTime.Now.ToString()); } return finalPrintString; }
And there here is a method that uses Regex to find and replace
/// <summary> /// Uses regex '\b' as suggested in //http://stackoverflow.com/questions/6143642/way-to-have-string-replace-only-hit-whole-words /// </summary> /// <param name="original"></param> /// <param name="wordToFind"></param> /// <param name="replacement"></param> /// <param name="regexOptions"></param> /// <returns></returns> private static string ReplaceWholeWord(string completeString, string variableToReplace, string replacement, RegexOptions regexOptions = RegexOptions.IgnoreCase) { var pattern = string.Format(@"\b{0}\b", variableToReplace); var ret = Regex.Replace(completeString, pattern, replacement, regexOptions); return ret; }
But again, to summarize, if I look at the string ret just before it returns, my {DateTime.Now} still says just that and was not replaced with the actual DateTime.Now. Also, I did inspect the pattern and replacement arguments and they are correct.
pattern shows as
\b{DateTime.Now}\b
and replacement shows as
4/4/2017 5:40PM