String 변수값에서 숫자 혹은 문자를 골라내서 삭제할 수 있는 간단한 메서드 예제
코드 출처 : Stack Overflow
function RemoveDecimal(const aString: string): string;
var
C:Char; Index:Integer;
begin
Result := '';
SetLength(Result, Length(aString));
Index := 1;
for C in aString do
begin
if not CharInSet(C, ['0' .. '9']) then
begin
Result[Index] := C;
Inc(Index);
end;
end;
SetLength(Result, Index-1);
end;
위 예제 코드에서 조건만 반대로 걸면, 숫자가 아니라 문자만 제거할 수도 있다.
procedure RemoveChar(var s: string);
var
i, j: Integer;
pc: PChar;
begin
j := 0;
pc := PChar(@s[1]);
for i := 0 to Length(s) - 1 do
begin
if not (pc[i] in ['0'..'9']) then
Inc(j)
else
pc[i - j] := pc[i];
end;
SetLength(s, Length(s) - j);
end;
var
타입으로 받고 있으므로, procedure
로 구현하였다.