首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

求高手解决,字符串包含有关问题

2012-09-10 
求高手解决,字符串包含问题。举例:字符串A是 a/b/c/d/e/f字符串B是 a/c/f字符串C是 b/e字符串D是 b/g/c怎么

求高手解决,字符串包含问题。
举例:
字符串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 := Trueend;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; 

热点排行