{"id":16928,"date":"2022-10-22T14:22:01","date_gmt":"2022-10-22T08:52:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/"},"modified":"2022-10-22T14:22:01","modified_gmt":"2022-10-22T08:52:01","slug":"solved-how-to-replace-tokens-on-a-string-template-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/","title":{"rendered":"[Solved] How to replace tokens on a string template? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-20651798\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"20651798\" data-parentid=\"20651754\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<h2>Short answer<\/h2>\n<p>I think it would be best to use a Regex.<\/p>\n<pre><code>var result = Regex.Replace(str, @\"{{(?&lt;Name&gt;[^}]+)}}\", m =&gt;\n{\n    return m.Groups[\"Name\"].Value; \/\/ Date, Time\n});\n<\/code><\/pre>\n<hr>\n<p>On c#-6.0 you can use:<\/p>\n<pre><code>string result = $\"Time: {DateTime.Now}\";\n<\/code><\/pre>\n<hr>\n<h2>String.Format &amp; IFormattable<\/h2>\n<p>However, there is a method for that already. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.string.format%28v=vs.110%29.aspx\">Documentation<\/a>.<\/p>\n<pre><code>String.Format(\"The current Date is: {0}, the time is: {1}\", date, time);\n<\/code><\/pre>\n<p>And also, you can use a class with <code>IFormattable<\/code>. I didn&#8217;t do performance tests but this one might be fast:<\/p>\n<pre><code>public class YourClass : IFormattable\n{\n    public string ToString(string format, IFormatProvider formatProvider)\n    {\n        if (format == \"Date\")\n            return DateTime.Now.ToString(\"yyyy\/MM\/d\");\n        if (format == \"Time\")\n            return DateTime.Now.ToString(\"HH:mm\");\n        if (format == \"DateTime\")\n            return DateTime.Now.ToString(CultureInfo.InvariantCulture);\n\n        return format;\n\n        \/\/ or throw new NotSupportedException();\n    }\n}\n<\/code><\/pre>\n<p>And use as<\/p>\n<pre><code>String.Format(\"The current Date is: {0:Date}, the time is: {0:Time}\", yourClass);\n<\/code><\/pre>\n<hr>\n<h2>Review of your code and detailed information<\/h2>\n<p>In your current code you are using<\/p>\n<pre><code>\/\/ match.Value = {{Date}}\nmatch.Value.Substring(2, match.Value.Length - 4).Replace(\" \", String.Empty);\n<\/code><\/pre>\n<p>Instead, if you look at my code above, I used the pattern<\/p>\n<pre><code>@\"{{(?&lt;Name&gt;[^}]+)}}\"\n<\/code><\/pre>\n<p>The syntax <code>(?&lt;SomeName&gt;.*)<\/code> means this is a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bs2twtah%28v=vs.110%29.aspx#named_matched_subexpression\">named group, you can check the documentation here.<\/a><\/p>\n<p>It allows you to access <code>match.Groups[\"SomeName\"].Value<\/code> which will be equivalent to the pattern with this group. So it would match two times, returning &#8220;Date&#8221; and then &#8220;Time&#8221;, so you don&#8217;t need to use <code>SubString<\/code>.<\/p>\n<p>Updating your code, it would be<\/p>\n<pre><code>private const string RegexIncludeBrackets = @\"{{(?&lt;Param&gt;.*?)}}\";\n\npublic static string ParseString(string input)\n{\n    return Regex.Replace(input, RegexIncludeBrackets, match =&gt;\n    {\n        string cleanedString = match.Groups[\"Param\"].Value.Replace(\" \", String.Empty);\n        switch (cleanedString)\n        {\n            case \"Date\":\n                return DateTime.Now.ToString(\"yyyy\/MM\/d\");\n            case \"Time\":\n                return DateTime.Now.ToString(\"HH:mm\");\n            case \"DateTime\":\n                return DateTime.Now.ToString(CultureInfo.InvariantCulture);\n            default:\n                return match.Value;\n        }\n    });\n}\n<\/code><\/pre>\n<p>To improve even more, you can have a static compiled Regex field:<\/p>\n<pre><code>private static Regex RegexTemplate = new Regex(@\"{{(?&lt;Param&gt;.*?)}}\", RegexOptions.Compiled);\n<\/code><\/pre>\n<p>And then use as<\/p>\n<pre><code>RegexTemplate.Replace(str, match =&gt; ...);\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to replace tokens on a string template? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Short answer I think it would be best to use a Regex. var result = Regex.Replace(str, @&#8221;{{(?&lt;Name&gt;[^}]+)}}&#8221;, m =&gt; { return m.Groups[&#8220;Name&#8221;].Value; \/\/ Date, Time }); On c#-6.0 you can use: string result = $&#8221;Time: {DateTime.Now}&#8221;; String.Format &amp; IFormattable However, there is a method for that already. Documentation. String.Format(&#8220;The current Date is: {0}, the &#8230; <a title=\"[Solved] How to replace tokens on a string template? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/\" aria-label=\"More on [Solved] How to replace tokens on a string template? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,3404],"class_list":["post-16928","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-template-engine"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to replace tokens on a string template? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to replace tokens on a string template? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Short answer I think it would be best to use a Regex. var result = Regex.Replace(str, @&quot;{{(?&lt;Name&gt;[^}]+)}}&quot;, m =&gt; { return m.Groups[&quot;Name&quot;].Value; \/\/ Date, Time }); On c#-6.0 you can use: string result = $&quot;Time: {DateTime.Now}&quot;; String.Format &amp; IFormattable However, there is a method for that already. Documentation. String.Format(&quot;The current Date is: {0}, the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-22T08:52:01+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to replace tokens on a string template? [closed]\",\"datePublished\":\"2022-10-22T08:52:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/\"},\"wordCount\":171,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"template-engine\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/\",\"name\":\"[Solved] How to replace tokens on a string template? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-22T08:52:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to replace tokens on a string template? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to replace tokens on a string template? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to replace tokens on a string template? [closed] - JassWeb","og_description":"[ad_1] Short answer I think it would be best to use a Regex. var result = Regex.Replace(str, @\"{{(?&lt;Name&gt;[^}]+)}}\", m =&gt; { return m.Groups[\"Name\"].Value; \/\/ Date, Time }); On c#-6.0 you can use: string result = $\"Time: {DateTime.Now}\"; String.Format &amp; IFormattable However, there is a method for that already. Documentation. String.Format(\"The current Date is: {0}, the ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-22T08:52:01+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to replace tokens on a string template? [closed]","datePublished":"2022-10-22T08:52:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/"},"wordCount":171,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","template-engine"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/","name":"[Solved] How to replace tokens on a string template? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-22T08:52:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-replace-tokens-on-a-string-template-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to replace tokens on a string template? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16928"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16928\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}