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