該示例演示了一個字符串擴展splitex例子,文章內含源代碼。
function SplitEx(const Str {需要拆分的文章}, Delimiters {拆分關鍵字,回車.?!等}: string): TStringList;
var
ss: WideString;
i, St: integer;
function IsDelimiter(const Delimiters, c: string): Boolean;
begin //判斷是否為拆分關鍵字
result := StrScan(PChar(Delimiters), c[1]) <> nil;
end;
begin
Result := TStringList.Create;
with Result do
begin
Clear; Sorted := True; Duplicates := dupIgnore;
end;
if Length(Str) < 1 then exit;
ss := Str; //雙字符支持,純英文可以去掉
St := -1;
for i := 1 to Length(ss) do
if IsDelimiter(Delimiters, ss[i]) then
if St <> -1 then
begin
Result.Add(Trim(Copy(ss, St, i - St)));
St := -1;
end
else
if St = -1 then St := i;
if St <> -1 then Result.Add(Copy(ss, St, Length(Str)));
end;
//操作演示
with SplitEx(Memo1.Text, ',,. ?! ' + #13#10) do
try
SaveToFile('c: emp_demo.txt');
finally
Free;
end;