Search code examples
androiddelphidelphi-xe7

how do i get my code worked with zerobased correctley?


i`am trying to Migrating my code to be worked on android so i read here http://docwiki.embarcadero.com/RADStudio/XE8/en/Migrating_Delphi_Code_to_Mobile_from_Desktop

and i understand i have not use pos or delete with zerobased on or i have to turn zero based off , i don't feel good about turning of zero based off

so i work around my code i changed p := Pos(Sep, S); To p := S.IndexOf(Sep, 0,0); but iam unable to use remove TStringHelper.Remove function instead of delete

  while (S <> '') and (ParamsCount < 10) do
    begin
      Inc(ParamsCount);
      p := S.IndexOf(Sep, 0,0);
      //p := Pos(Sep, S);
      if p = 0 then
        Params[ParamsCount] := S
      else
      begin
        Params[ParamsCount] := Copy(S, 1, P - 1);
       TStringHelper.Remove(S, 1, P + 4); // here how do i use remove its only have integer parameter how to use Remove instead of Delete 
       //Delete(S, 1, P + 4);
      end;
    end;
  end;

Solution

  • There are a few issue here. First of all, the ZEROBASEDSTRINGS directive doesn't influence any of this code because you don't use the [] operator. However, I recommend that you leave ZEROBASEDSTRINGS as ON and embrace the new ways.

    The string helper use zero based indexing exclusively and it will reduce confusion if you follow suit in your code.

    As for the details:

    • Remove returns a new string, rather than modifying its argument.
    • IndexOf returns -1 to indicate no match found.
    • Use Substring rather than Copy. The latter uses old school one based indexing. You should shun all the old SysUtils functions and use the helper exclusively.

    I'd write the code like this:

    p := S.IndexOf(Sep);
    if p = -1 then
      Params[ParamsCount] := S
    else
    begin
      Params[ParamsCount] := S.Substring(0, P);
      S := S.Remove(0, P + 3);
    end;
    

    Your code was a little messy and so there may be some errors in the above. I was trying to decipher the intent from the commented out code. However, the code above demonstrates the style that you should adopt.