これは、「英数字」と「パンカット文字」の定義によって異なります。
たとえば、句読文字のセットを定義する場合
const
PUNCT = ['.', ',', ':', ';', '-', '!', '?'];
他のすべての文字を英数字と見なすと、次のことができます。
function RemovePunctuation(const Str: string): string;
var
ActualLength: integer;
i: Integer;
const
PUNCT = ['.', ',', ':', ';', '-', '!', '?'];
begin
SetLength(result, length(Str));
ActualLength := 0;
for i := 1 to length(Str) do
if not (Str[i] in PUNCT) then
begin
inc(ActualLength);
result[ActualLength] := Str[i];
end;
SetLength(result, ActualLength);
end;
この関数は、文字列を文字列に変換します。代わりに文字列を文字の配列に変換したい場合は、
type
CharArray = array of char;
function RemovePunctuation(const Str: string): CharArray;
var
ActualLength: integer;
i: Integer;
const
PUNCT = ['.', ',', ':', ';', '-', '!', '?'];
begin
SetLength(result, length(Str));
ActualLength := 0;
for i := 1 to length(Str) do
if not (Str[i] in PUNCT) then
begin
result[ActualLength] := Str[i];
inc(ActualLength);
end;
SetLength(result, ActualLength);
end;
(はい、Delphiでは、文字列は1ベースのインデックスを使用しますが、配列は0ベースのインデックスを使用します。これは歴史的な理由によるものです。)