Frage

I have a problem with removing the questionmarks. I don't want them in my SQL-Database. But my code isn't working, and I don't know where the problems are.

function RemoveQMark(sWork: String): String;
begin
  Delete(sWork, 2, 4);
  Delete(sWork, Length, 2);
  Result := sWork;
end;

I want to remove the ? at the first and last position.

War es hilfreich?

Lösung

There is no need to use Delete on the passed in string. Simply use the Delphi Copy function to copy all but the first and last characters directly to the result:

function RemoveQMark(const sWork: String): String;
begin
  Result := Copy(sWork, 2, Length(sWork) - 2);
end;

Using const on string arguments allows the compiler to generate more efficient code. (Without const, the strings reference count is incremented at the start of the function and decremented at the end (within a try...finally block)).

Andere Tipps

function RemoveQMark(sWork: String): String;
begin
  Delete(sWork, 1, 1);
  Delete(sWork, Length(sWork), 1);
  Result := sWork;
end;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top