質問

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.

役に立ちましたか?

解決

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)).

他のヒント

function RemoveQMark(sWork: String): String;
begin
  Delete(sWork, 1, 1);
  Delete(sWork, Length(sWork), 1);
  Result := sWork;
end;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top