<pre class="brush: delphi;">///全角対応トリム
///トリム対象文字列
/// トリム処理後文字列
const
  SPACE = ' ';
  ZENSPC = '　';

function LTrim(s: String): String;
var
  i, iLast: integer;
begin
  i := 1;
  iLast := Length(s);

  while true do
  begin
    if (Copy(s, i, 1) = SPACE) then                         // 半角空白
      inc(i)
{$IFDEF UNICODE}
    else if (Copy(s, i, 1) = ZENSPC) then                   // 全角空白
      inc(i)
{$ELSE}
    else if ((Length(s) &gt;= 2) and (Copy(s, i, 2) = ZENSPC)) then  // 全角空白
      inc(i, 2)
{$ENDIF}
    else
      break;
  end;
  Result := Copy(s, i, iLast - i +1);
end;

function RTrim(s: String): String;
var
  i: integer;
begin
  i := Length(s);
  while (i &gt; 0) do
  begin
    if (Copy(s, i, 1) = SPACE) then                         // 半角空白
      dec(i)
{$IFDEF UNICODE}
    else if (Copy(s, i, 1) = ZENSPC) then                   // 全角空白
      dec(i)
{$ELSE}
    else if ((Length(s) &gt;= 2) and (Copy(s, i-1, 2) = ZENSPC)) then  // 全角空白
      dec(i, 2)
{$ENDIF}
    else
      break;
  end;
  Result := Copy(s, 1, i);
end;

function ZTrim(s: String): String;
begin
  Result := RTrim(LTrim(s));
end;
</pre>