Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TStringHelper is not returning the correct results

I'm using the TStringHelper in a Win32 Application, but when I try to access a particular char or get a substring the values returned are not the same If I use the equivalent old string functions.

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

var
  i : Integer;
  s : string;
begin
  try
    i:=12345678;
    Writeln(i.ToString().Chars[1]);  // returns 2
    Writeln(i.ToString().Substring(1)); //returns 2345678

    s:=IntToStr(i);
    Writeln(s[1]); //returns 1
    Writeln(Copy(s,1,Length(s)));//returns 12345678
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

The question is Why the TStringHelper functions are not equivalent to the old string functions?

like image 630
Salvador Avatar asked Dec 07 '22 06:12

Salvador


1 Answers

This is because all the methods and properties of the System.SysUtils.TStringHelper are zero based index, this helper was compiler with the {$ZEROBASEDSTRINGS ON} directive. you can find more info in the System.SysUtils.TStringHelper documentation.

like image 164
RRUZ Avatar answered Dec 26 '22 17:12

RRUZ