Trimを使って、下記の処理を行うと、Edit1.Textの両側の半角の空白は削除されるのですが、全角の空白は削除されません。全角の空白も削除するにはどうしたらいいのでしょうか?他に関数があるのでしょうか?
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:= Trim(Edit1.Text);
end;
end.
「全角 空白」 でまずは検索して下さい。
>他に関数があるのでしょうか?
なければ自前で、もちろん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;
無限ループの危険を回避。
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;
つくっとります。
指定文字をTrimする関数
http://delfusa.main.jp/delfusafloor/technic/technic/044_TrimChar.html
TrimChar(Edit1.Text, 半角スペース+全角スペース+制御文字群);
こんな感じで指定するとよいかと。
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);
このようにすると動きますよ。
ツイート | ![]() |