{"id":69,"date":"2012-09-08T15:38:53","date_gmt":"2012-09-08T15:38:53","guid":{"rendered":"http:\/\/www.notnull.ro\/log\/?p=69"},"modified":"2014-01-20T21:49:26","modified_gmt":"2014-01-20T21:49:26","slug":"split-a-string-by-substring-multichars-in-tcl","status":"publish","type":"post","link":"https:\/\/notnull.ro\/log\/split-a-string-by-substring-multichars-in-tcl\/","title":{"rendered":"split a string by substring (multichars) in tcl"},"content":{"rendered":"<p>Few time ago I had a tcl chalange that in other languages is straight forward: split a string using as separator a substring.<br \/>\nIn tcl there is a split function: [split $myStr $subStr] but, by default it splits by any char of $subStr.<br \/>\nTo be more precise let&#8217;s assume that we have a string $my_str like<\/p>\n<pre><code>\"set1 line1\r\nset1 line 2\r\nset 1 line 3\r\n\r\nset2 line 1\r\nset2 line 2\r\n\r\nset3 line1\r\nset3 line2\r\nset3 line3\"<\/code>\r\n<\/pre>\n<p>and we want to split by empty line (&#8220;\\n\\n&#8221;).<\/p>\n<p>If we try soemething like<br \/>\n<code> set list_str  [split $my_str \"\\n\\n\"]<\/code><br \/>\nthe $list_str is<br \/>\n<code>{set1 line1 } {set1 line 2} {set 1 line 3} {} {set2 line 1} {set2 line 2} {} {set3 line1} {set3 line2} {set3 line3}<\/code><br \/>\nit is the same thing as <code> set list_str  [split $my_str \"\\n\"]<\/code> .<\/p>\n<p>The trick is to replace the &#8220;\\n\\n&#8221; sequence in $my_str with one single char , a char which does not exists in $my_str, and the to split the result by this single char.<br \/>\nAnd one char which probable does not exists in $my_str is something like &#8220;\\x00&#8221;.<\/p>\n<p>So, a line like<br \/>\n<code> set list_str [split [string map [list \"\\n\\n\" \"\\x00\"] $my_str] \"\\x00\"] <\/code><br \/>\nis exactly what you need<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Few time ago I had a tcl chalange that in other languages is straight forward: split a string using as separator a substring. In tcl there is a split function: [split $myStr $subStr] but, by default it splits by any char of $subStr. To be more precise let&#8217;s assume that we have a string $my_str <a href='https:\/\/notnull.ro\/log\/split-a-string-by-substring-multichars-in-tcl\/' class='excerpt-more'>&#8230; Read Full Article<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[28,26,27,84],"class_list":["post-69","post","type-post","status-publish","format-standard","hentry","category-tcl","tag-multichars","tag-split","tag-string-substring","tag-tcl","category-20-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":10,"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/posts\/69\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notnull.ro\/log\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}