{"id":11833,"date":"2022-09-28T17:32:39","date_gmt":"2022-09-28T12:02:39","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/"},"modified":"2022-09-28T17:32:39","modified_gmt":"2022-09-28T12:02:39","slug":"solved-extract-string-token-objects-from-string","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/","title":{"rendered":"[Solved] Extract string-token objects from string?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-66294625\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"66294625\" data-parentid=\"66282526\" data-score=\"3\" 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<p>On request, here is how I would do this myself:<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png\"><img decoding=\"async\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png\" alt=\"Screenshot of application\"><\/a><\/p>\n<p>First, I want to create a function that performs this operation, so it can be reused every time we need to do this.<\/p>\n<p>I could have this function return or populate a <code>TList&lt;TWordRec&gt;<\/code>, but then it would be tiresome to work with it, because the user of the function would then need to add <code>try..finally<\/code> blocks every time the function is used. Instead, I let it return a <code>TArray&lt;TWordRec&gt;<\/code>. By definition, this is simply <code>array of TWordRec<\/code>, that is, a dynamic array of <code>TWordRec<\/code>s.<\/p>\n<p>But how to efficiently populate such an array? We all know you shouldn&#8217;t increase the length of a dynamic array one element at a time; besides, that requires a lot of code. Instead, I populate a local <code>TList&lt;TWordRec&gt;<\/code> and then, as a last step, create an array from it:<\/p>\n<pre class=\"lang-pascal prettyprint-override\"><code>type\n  TPhraseMatch = record\n    Position: Integer;\n    Text: string;\n  end;\n\nfunction GetPhraseMatches(const AText, APhrase: string): TArray&lt;TPhraseMatch&gt;;\nbegin\n\n  var TextLower := AText.ToLower;\n  var PhraseLower := APhrase.ToLower;\n\n  var List := TList&lt;TPhraseMatch&gt;.Create;\n  try\n\n    var p := 0;\n    repeat\n      p := Pos(PhraseLower, TextLower, p + 1);\n      if p &lt;&gt; 0 then\n      begin\n        var Match: TPhraseMatch;\n        Match.Position := p - 1 {since the OP wants 0-based string indexing};\n        Match.Text := Copy(AText, p, APhrase.Length);\n        List.Add(Match);\n      end;\n    until p = 0;\n\n    Result := List.ToArray;\n\n  finally\n    List.Free;\n  end;\n\nend;\n<\/code><\/pre>\n<p>Notice that I chose an alternative to the regular expression approach, just for educational reasons. I also believe this approach is faster. Also notice how easy it is to work with the <code>TList&lt;TWordRec&gt;<\/code>: it&#8217;s just like a <code>TStringList<\/code> but with word records instead of strings!<\/p>\n<p>Now, let&#8217;s use this function:<\/p>\n<pre class=\"lang-pascal prettyprint-override\"><code>procedure TWordFinderForm.ePhraseChange(Sender: TObject);\nbegin\n\n  lbMatches.Items.BeginUpdate;\n  try\n    lbMatches.Items.Clear;\n    for var Match in GetPhraseMatches(mText.Text, ePhrase.Text) do\n      lbMatches.Items.Add(Match.Position.ToString + ':'#32 + Match.Text)\n  finally\n    lbMatches.Items.EndUpdate;\n  end;\n\nend;\n<\/code><\/pre>\n<p>Had I not chosen to use a function, but placed all code in one block, I could have iterated over the <code>TList&lt;TWordRec&gt;<\/code> in exactly the same way:<\/p>\n<pre class=\"lang-pascal prettyprint-override\"><code>for var Match in List do\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">10<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Extract string-token objects from string? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] On request, here is how I would do this myself: First, I want to create a function that performs this operation, so it can be reused every time we need to do this. I could have this function return or populate a TList&lt;TWordRec&gt;, but then it would be tiresome to work with it, because &#8230; <a title=\"[Solved] Extract string-token objects from string?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/\" aria-label=\"More on [Solved] Extract string-token objects from string?\">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":[740,3229,362,3026],"class_list":["post-11833","post","type-post","status-publish","format-standard","hentry","category-solved","tag-delphi","tag-delphi-10-4-sydney","tag-string","tag-tokenize"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Extract string-token objects from string? - 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-extract-string-token-objects-from-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Extract string-token objects from string? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] On request, here is how I would do this myself: First, I want to create a function that performs this operation, so it can be reused every time we need to do this. I could have this function return or populate a TList&lt;TWordRec&gt;, but then it would be tiresome to work with it, because ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-28T12:02:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png\" \/>\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-extract-string-token-objects-from-string\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Extract string-token objects from string?\",\"datePublished\":\"2022-09-28T12:02:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/\"},\"wordCount\":227,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png\",\"keywords\":[\"delphi\",\"delphi-10.4-sydney\",\"string\",\"tokenize\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/\",\"name\":\"[Solved] Extract string-token objects from string? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png\",\"datePublished\":\"2022-09-28T12:02:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#primaryimage\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Extract string-token objects from string?\"}]},{\"@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=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Extract string-token objects from string? - 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-extract-string-token-objects-from-string\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Extract string-token objects from string? - JassWeb","og_description":"[ad_1] On request, here is how I would do this myself: First, I want to create a function that performs this operation, so it can be reused every time we need to do this. I could have this function return or populate a TList&lt;TWordRec&gt;, but then it would be tiresome to work with it, because ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/","og_site_name":"JassWeb","article_published_time":"2022-09-28T12:02:39+00:00","og_image":[{"url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png","type":"","width":"","height":""}],"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-extract-string-token-objects-from-string\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Extract string-token objects from string?","datePublished":"2022-09-28T12:02:39+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/"},"wordCount":227,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png","keywords":["delphi","delphi-10.4-sydney","string","tokenize"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/","url":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/","name":"[Solved] Extract string-token objects from string? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#primaryimage"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png","datePublished":"2022-09-28T12:02:39+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#primaryimage","url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/09\/Solved-Extract-string-token-objects-from-string.png"},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-extract-string-token-objects-from-string\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Extract string-token objects from string?"}]},{"@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=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","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\/11833","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=11833"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11833\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=11833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=11833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=11833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}