求高手解决,字符串包含有关问题
求高手解决,字符串包含问题。
举例:
字符串A是 a/b/c/d/e/f
字符串B是 a/c/f
字符串C是 b/e
字符串D是 b/g/c
怎么用Delphi判断:字符串B包含于A内,字符串C也是包含于A内,字符串D不包含在A内呢?
最好有个代码供我研究,多谢大家啦!
------解决方案--------------------
用循环判断把 简单例子. B 包含在A中.
function funcA(A, B: string): Boolean;
var
i:Integer;
AList: TStringList;
begin
Result := True;
AList := TStringList.Create;
AList.Delimiter := '/';
AList.DelimitedText := B;
for i := 0 to AList.Count - 1 do
beign
Result := pos(AList[I], A) > 0;
if not Result then Break;
end;
end;
------解决方案--------------------
举例:
字符串A是 a/b/c/d/e/f
字符串B是 a/c/f
字符串C是 b/e
字符串D是 b/g/c
怎么用Delphi判断:字符串B包含于A内,字符串C也是包含于A内,字符串D不包含在A内呢?
最好有个代码供我研究,多谢大家啦!
------解决方案--------------------
用循环判断把 简单例子. B 包含在A中.
function funcA(A, B: string): Boolean;
var
i:Integer;
AList: TStringList;
begin
Result := True;
AList := TStringList.Create;
AList.Delimiter := '/';
AList.DelimitedText := B;
for i := 0 to AList.Count - 1 do
beign
Result := pos(AList[I], A) > 0;
if not Result then Break;
end;
end;
------解决方案--------------------
- Delphi(Pascal) code
function Contains(const Str, SubStr: string): Boolean; var P1, P2: PChar; begin P1 := PChar(SubStr); while P1^ <> #0 do begin P2 := PChar(Str); while P2^ <> #0 do begin if P2^ = P1^ then Break; Inc(P2) end; if P2^ = #0 then begin Result := False; Exit end; Inc(P1); end; Result := True end; procedure TForm1.FormCreate(Sender: TObject); begin if Contains('a/b/c/d/e/f', 'b/c') then ShowMessage('') end;
------解决方案--------------------
一楼的代码改进一下就可以了
- Delphi(Pascal) code
function funcA(A, B: string): Boolean; var i:Integer; AList: TStringList; begin Result := True; AList := TStringList.Create; AList.Delimiter := '/'; AList.DelimitedText := B; for i := 0 to AList.Count - 1 do begin Result := pos(AList[I], A) > 0; if not Result then Break [color=#FF99CC]else A:=copy(A,pos(AList[I], A)+1,length(A)-pos(AList[I], A));[/color] //比如:该字符串a/c/c/d是不在字符串A中的,是吧? end; end;