字符串中有顿号隔开,怎样删除相同字符?
字符串中有顿号隔开,怎样删除相同字符?(删除后面相同的字符,包括顿号)
例如a、bb、cc、bb、abc
是变为a、bb、cc、abc
[解决办法]
procedure TForm1.Button1Click(Sender: TObject);const constr:String = 'a\bb\cc\bb\abc';vars,temp:TStrings;i :Integer;begins := TStringList.Create;temp := TStringList.Create;s.Delimiter := '\';s.DelimitedText := constr;for i := 0 to s.Count-1 do if temp.IndexOf(s.Strings[i]) = -1 then temp.Add(s.Strings[i]);s.Free; Memo1.Lines := temp;temp.Free;end;
[解决办法]
s.sorted:=true;
for i:= s.count-1 downto 0 do
if s.indexof(s.strings[i]) <> i then
s.delete(i);
[解决办法]
procedure TForm1.Button1Click(Sender: TObject);var SourceStr,ResultStr,TmpStr:string; P:pchar; i:integer;begin SourceStr:='a、bb、cc、bb、abc'; i:=pos('、',SourceStr); while i>0 do begin TmpStr:=copy(SourceStr,1,i); p:=StrPos(PChar(ResultStr), PChar(TmpStr)); if p='' then ResultStr:=ResultStr+TmpStr; delete(SourceStr,1,i); i:=pos('、',SourceStr); end; showmessage(ResultStr+SourceStr);end;
[解决办法]
折半查找是很快,但要求是有序的。Sorted会打乱原有的顺序。