Trimについて


田村  2008-11-14 19:01:33  No: 32596  IP: 192.*.*.*

Trimを使って、下記の処理を行うと、Edit1.Textの両側の半角の空白は削除されるのですが、全角の空白は削除されません。全角の空白も削除するにはどうしたらいいのでしょうか?他に関数があるのでしょうか?

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text:= Trim(Edit1.Text);
end;

end.

編集 削除
TS  2008-11-14 21:25:35  No: 32597  IP: 192.*.*.*

「全角  空白」  でまずは検索して下さい。

編集 削除
そうねぇ  2008-11-14 22:59:23  No: 32598  IP: 192.*.*.*

>他に関数があるのでしょうか?
なければ自前で、もちろんShiftJIS文字列限定


function TrimEx(const S: string): string;
var
  I, L: Integer;
begin
  L := Length(S);
  I := 1;
  while (I <= L) and ((S[I] <= ' ')or(S[I] = #$81)) do begin
   if (S[I] = #$81) then if (S[I+1] = #$40) then Inc(I) else Dec(I);
   Inc(I);
  end;
  if I > L then Result := '' else
  begin
   while (S[L] <= ' ')or(S[L] = #$40) do begin
    if (S[L] = #$40) then if (S[L-1] = #$81) then Dec(L) else Inc(L);
    Dec(L);
   end;
   Result := Copy(S, I, L - I + 1);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := TrimEx(#9#10'    123456      ');
end;

編集 削除
添削 その1  2008-11-14 23:33:47  No: 32599  IP: 192.*.*.*

無限ループの危険を回避。

function TrimEx(const S: string): string;
var
  I, L: Integer;
begin
  L := Length(S);
  I := 1;
  while (I <= L) and ((S[I] <= ' ')or(S[I] = #$81)) do begin
   if (S[I] = #$81) then if (S[I+1] = #$40) then Inc(I) else break;
   Inc(I);
  end;
  if I > L then Result := '' else
  begin
   while (S[L] <= ' ')or(S[L] = #$40) do begin
    if (S[L] = #$40) then if (S[L-1] = #$81) then Dec(L) else break;
    Dec(L);
   end;
   Result := Copy(S, I, L - I + 1);
  end;
end;

編集 削除
Fusa  2008-11-17 19:05:40  No: 32600  IP: 192.*.*.*

つくっとります。

指定文字をTrimする関数
http://delfusa.main.jp/delfusafloor/technic/technic/044_TrimChar.html

TrimChar(Edit1.Text, 半角スペース+全角スペース+制御文字群);
こんな感じで指定するとよいかと。

編集 削除
Fusa  2008-11-18 01:28:42  No: 32601  IP: 192.*.*.*

const CtrlCharTbl: String =
    #$01#$02#$03#$04#$05#$06#$07#$08#$09#$0A#$0B#$0C#$0D#$0E#$0F+
    #$10#$11#$12#$13#$14#$15#$16#$17#$18#$19#$1A#$1B#$1C#$1D#$1E#$1F;

TrimChar(Edit1.Text, ' '+'  '+CtrlCharTbl);

このようにすると動きますよ。

編集 削除