{"id":171,"date":"2013-04-20T14:48:51","date_gmt":"2013-04-20T12:48:51","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=171"},"modified":"2013-04-24T18:15:30","modified_gmt":"2013-04-24T16:15:30","slug":"indexing-of-strings","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2013\/04\/20\/indexing-of-strings\/","title":{"rendered":"Indexing of Strings"},"content":{"rendered":"<p>As everyone knows Delphi strings are 1-based. This means that they are starting at the index 1 and ending at the index &#8220;Length()&#8221;. The reason is that Borland made the huge strings compatible to the old ShortStrings that had a length byte at the position 0.<\/p>\n<p>I have to admit that I&#8217;m not happy about this fact since every TList and dynamic array are 0-based. All other important programming languages also have 0-based strings. I guess and also have the impression that EMBT will change this.<\/p>\n<p>Certainly this will break all the old Delphi code. But for new code that you will write in the future you can do it in the way that it will work with 0- and 1-based strings.<\/p>\n<p>Let&#8217;s look at an example:<\/p>\n<pre class=\"lang:delphi decode:true \" title=\"Example for 1-based strings\" >...\r\nvar\r\n  I: Integer;\r\n  sBuffer: string;\r\nbegin\r\n  sBuffer := 'Hallo World!';\r\n  for I := 1 to Length(sBuffer) do\r\n    Writeln(sBuffer[I]);\r\n  Readln;\r\nend.\r\n...<\/pre>\n<p>As you can see there is the &#8220;1&#8221; and the &#8220;Length&#8221;. This code only works with 1-based strings. With the help of Low and High you can write it in a more flexible way:<\/p>\n<pre class=\"lang:delphi decode:true \" title=\"Flexible Example for strings\" >\r\n...\r\nvar\r\n  I: Integer;\r\n  sBuffer: string;\r\nbegin\r\n  sBuffer := 'Hallo World!';\r\n  for I := Low(sBuffer) to High(sBuffer) do\r\n    Writeln(sBuffer[I]);\r\n  Readln;\r\nend.\r\n...<\/pre>\n<p>Please consider that Low works for strings in general. This means that you can also write the code in this way:<\/p>\n<pre class=\"lang:delphi decode:true \" title=\"Flexible Example for strings\" >\r\n...\r\nvar\r\n  I: Integer;\r\n  sBuffer: string;\r\nbegin\r\n  sBuffer := 'Hallo World!';\r\n  for I := Low(string) to High(sBuffer) do\r\n    Writeln(sBuffer[I]);\r\n  Readln;\r\nend.\r\n...<\/pre>\n<p>With the help of the new record helpers you can also write it more object-oriented:<\/p>\n<pre class=\"lang:delphi decode:true \" title=\"Flexible Example for strings\" >\r\n...\r\ntype\r\n  TStringHelper = record helper for string\r\n  public\r\n    function High: Integer; inline;\r\n    class function Low: Integer; static; inline;\r\n  end;\r\n\r\n{ TStringHelper }\r\n\r\nfunction TStringHelper.High: Integer;\r\nbegin\r\n  Result := System.High(Self);\r\nend;\r\n\r\nclass function TStringHelper.Low: Integer;\r\nbegin\r\n  Result := System.Low(string);\r\nend;\r\n\r\nvar\r\n  I: Integer;\r\n  sBuffer: string;\r\nbegin\r\n  sBuffer := 'Hallo World!';\r\n  for I := sBuffer.Low to sBuffer.High do\r\n    Writeln(sBuffer[I]);\r\n  Readln;\r\nend.\r\n...<\/pre>\n<p>Let&#8217;s see which features EMBT will bring to XE4 in the future, please stay tuned&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As everyone knows Delphi strings are 1-based. This means that they are starting at the index 1 and ending at the index &#8220;Length()&#8221;. The reason is that Borland made the huge strings compatible to the old ShortStrings that had a &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2013\/04\/20\/indexing-of-strings\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-171","post","type-post","status-publish","format-standard","hentry","category-tipsandtricks","tag-delphi"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":13,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":200,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/171\/revisions\/200"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}