Trimについて


田村  2008-11-15 04:01:33  No: 32596

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

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

end.


TS  2008-11-15 06:25:35  No: 32597

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


そうねぇ  2008-11-15 07:59:23  No: 32598

>他に関数があるのでしょうか?
なければ自前で、もちろん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-15 08:33:47  No: 32599

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

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-18 04:05:40  No: 32600

つくっとります。

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

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


Fusa  2008-11-18 10:28:42  No: 32601

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

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


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加