第1个回答 2008-03-13
//建立新的单元
//单元代码如下
unit Unit2;
interface
uses
Classes;
type
TMyText = class(TObject)
private
SL: TStringList;
procedure SetText(Value: string);
function GetText: string;
public
constructor Create;
destructor Destroy; override;
function Add(text: string): Boolean; //加入字符,成功返回真
property Text: String read GetText write SetText;
end;
implementation
procedure StringToList(SL: TStringList; S: string);
procedure _SortString(S1: string);
var
I: integer;
temp: string;
begin
repeat
I := pos(#$20, S1);
if I = 0 then i := length(S1) + 1;
temp := copy(S1, 1, I - 1);
S1 := copy(S1, I + 1, length(S1));
if (temp <> '') and (SL.IndexOf(temp) = -1) then
SL.Add(temp);
until (I = 0) or (S1 = '');
end;
var
I: Integer;
temp: string;
begin
repeat
I := pos(sLineBreak, S);
if I <> 0 then
temp := copy(S, 1, I - 1)
else
temp := S;
_SortString(temp);
S := copy(S, I + length(sLineBreak), length(S));
until I = 0;
SL.Sort;
end;
procedure TMyText.SetText(Value: string);
begin
SL.Clear;
StringToList(SL, Value);
end;
function TMyText.GetText: string;
var
I: Integer;
begin
SL.Sort;
for I := 0 to SL.Count - 1 do
begin
result := result + SL[i] + #$20;
if (I <> 0) and ((I + 1) mod 3 = 0) then
result := result + sLineBreak;
end;
end;
function TMyText.Add(text: string): Boolean;
begin
result := false;
if SL.IndexOf(text) = -1 then
begin
SL.Add(text);
result := True;
end;
end;
constructor TMyText.Create;
begin
inherited Create;
SL := TStringList.Create;
end;
destructor TMyText.Destroy;
begin
SL.Free;
inherited;
end;
end.
//------------------------------------------
在要引用TMyText的单元上的uses 中加 Unit2
如:
uses
unit2;
var
MyText: TMyText;
begin
MyText := TMyText.Create;
try
MyText.Text := Memo1.Text;
if not MyText.Add('111') then
MessageBox(Handle, '已存在,请重新输入!', '', 0);
Memo2.Text := MyText.Text; //得到过滤后的TEXT
finally
MyText.Free;
end;