
{"id":5265,"date":"2025-05-19T12:30:33","date_gmt":"2025-05-19T16:30:33","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=5265"},"modified":"2025-05-19T12:39:37","modified_gmt":"2025-05-19T16:39:37","slug":"typescript-this-is-just-awesome-substr-is-not-the-same-as-substring","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=5265","title":{"rendered":"Typescript: this is just awesome. Substr is not the same as substring"},"content":{"rendered":"<p><i>In TypeScript (and JavaScript), both substr and substring are methods used to extract parts of a string, but they behave differently.<\/i><\/p>\n<p>This probably does not beat standard C++ <a href=\"https:\/\/ikriv.com\/blog\/?p=5213\">class mt19937<\/a>, but it is still awesome.<\/p>\n<p>The TL;DR is that <code>substr<\/code> takes start and <b>length<\/b>, whereas <code>substring<\/code> takes start and <b>end<\/b>. There are other, less critical differences, but this is the main one. But wait, that&#8217;s not all! There is also <code>slice()<\/code> that behaves <i>almost<\/i> like <code>substring<\/code>, but the end index is exclusive. Wow.<\/p>\n<p>To be honest, I am not sure how I would call all those methods, but having 3 different methods to get a substring is just wow.<\/p>\n<table>\n<thead>\n<tr>\n<th>Method<\/th>\n<th>Second Parameter<\/th>\n<th>Negative Start?<\/th>\n<th>Negative End\/Length?<\/th>\n<th>Swaps Indices?<\/th>\n<th>Preferred?<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>substr(start, length)<\/code><\/td>\n<td>Length of the substring<\/td>\n<td>\u2705 Yes<\/td>\n<td>\u274c No (treated as 0)<\/td>\n<td>\u274c Deprecated<\/td>\n<td>\u274c No<\/td>\n<\/tr>\n<tr>\n<td><code>substring(start, end)<\/code><\/td>\n<td>Inclusive end index<\/td>\n<td>\u274c No (treated as 0)<\/td>\n<td>\u274c No (treated as 0)<\/td>\n<td>\u2705 Yes<\/td>\n<td>\u26a0\ufe0f Legacy<\/td>\n<\/tr>\n<tr>\n<td><code>slice(start, end)<\/code><\/td>\n<td>Exclusive end index<\/td>\n<td>\u2705 Yes<\/td>\n<td>\u2705 Yes<\/td>\n<td>\u274c No<\/td>\n<td>\u2705 Recommended<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>In TypeScript (and JavaScript), both substr and substring are methods used to extract parts of a string, but they behave differently. This probably does not beat standard C++ class mt19937, <a href=\"https:\/\/ikriv.com\/blog\/?p=5265\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[29],"tags":[],"class_list":["entry","author-ikriv","post-5265","post","type-post","status-publish","format-standard","category-typescript"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5265"}],"version-history":[{"count":6,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5265\/revisions"}],"predecessor-version":[{"id":5271,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5265\/revisions\/5271"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}