星号字符串比较(200分)

  • 主题发起人 主题发起人 payer
  • 开始时间 开始时间
P

payer

Unregistered / Unconfirmed
GUEST, unregistred user!
星号为通配符,这个是VC的程序,怎样转为 DELPHI?

inline BOOL CMainFrame::UrlFilterStar(CStringArray &pattern, CString url)
{
CString str, tmp, tmp2;

try{

int i = 0, i2=0, start = 0, start2 = 0;//start - str, start2 - tmp
BOOL cmode = TRUE
//compare mode;
BOOL isame = FALSE;
BOOL final = FALSE;
int p = 0;
while(p<=pattern.GetUpperBound() && !isame)
{
str = pattern.GetAt(p);
//star filter
i = 0
i2 = 0
start = 0
start2 = 0;
isame = TRUE;cmode = TRUE;final = FALSE;
while(start<str.GetLength() && isame)
{
//get the string before *
i=str.Find('*', start);
if(i<0)
{
tmp2 = str.Mid(start);
i=str.GetLength();
final = TRUE;
}
else
tmp2 = str.Mid(start,i-start);
if(tmp2!=&quot;&quot;)
{
if(cmode)
{
tmp = url.Mid(start2, i-start);
if(tmp != tmp2)
isame = FALSE;
}
else if(final)
{
tmp = url.Right(tmp2.GetLength());
if(tmp != tmp2)
isame = FALSE;
}
else
{
i2=url.Find(tmp2, start2);
if( i2<0)
isame = FALSE;
}
}
cmode = FALSE;
start = i+1;
start2 = i2 + tmp2.GetLength();
}
p++;
}
return isame;
}
catch(...)
{
return FALSE;
}
}
 
既然*是通配符,肯定在设置这种过滤操作的时候会提供转义符的,就像C++类语言里面的转义符一样.你可以提供一个/*表示真实的*,而遇见单个*就是通配符
 
function SearchEngine(Str1, Str2: string
isCase: Boolean): Boolean;

function SameName(N1, N2: string): Boolean;
var
P1, P2: Byte;
Match: Boolean;
begin
P1 := 1;
P2 := 1;
Match := True;
if (Length(N1) = 0) and (Length(N2) = 0) then
Match := True
else if Length(N1) = 0 then
if N2[1] = '*' then
Match := True
else
Match := False
else if Length(N2) = 0 then
if N1[1] = '*' then
Match := True
else
Match := False;

while (Match = True) and (P1 <= Length(N1)) and (P2 <= Length(N2)) do
if (N1[P1] = '?') or (N2[P2] = '?') then begin
Inc(P1);
Inc(P2);
end
else
if N1[P1] = '*' then begin
Inc(P1);
if P1 <= Length(N1) then begin
while (P2 <= Length(N2)) and
not SameName(Copy(N1, P1, Length(N1) - P1 + 1),
Copy(N2, P2, Length(N2) - P2 + 1)) do
Inc(P2);
if P2 > Length(N2) then
Match := False
else begin
P1 := Succ(Length(N1));
P2 := Succ(Length(N2));
end;
end
else
P2 := Succ(Length(N2));
end
else
if N2[P2] = '*' then begin
Inc(P2);
if P2 <= Length(N2) then begin
while (P1 <= Length(N1)) and not SameName(Copy(N1, P1, Length(N1)
- P1 + 1), Copy(N2, P2, Length(N2) - P2 + 1)) do
Inc(P1);
if P1 > Length(N1) then
Match := False
else begin
P1 := Succ(Length(N1));
P2 := Succ(Length(N2));
end;
end
else
P1 := Succ(Length(N1));
end
else if N1[P1] = N2[P2] then begin
Inc(P1);
Inc(P2);
end
else
Match := False;
if P1 > Length(N1) then begin
while (P2 <= Length(N2)) and (N2[P2] = '*') do
Inc(P2);
if P2 <= Length(N2) then
Match := FALSE;
end;
if P2 > Length(N2) then begin
while (P1 <= Length(N1)) and (N1[P1] = '*') do
Inc(P1);
if P1 <= Length(N1) then
Match := False;
end;
SameName := Match;
end;

begin
if not isCase then begin
Str1 := Uppercase(Str1);
Str2 := Uppercase(Str2);
end;
SearchEngine := SameName(Str1, Str2);
end;
 
这个是我很久以前收藏的,感觉很不错
支持* 和 ? 通配符,
参数为两个待比较的字符串,是否区分大小写
 
我想做到的是
'我是人,哈哈' = '我*哈'
 
if SearchEngine('我是人,哈哈', '我*哈', False) then
ShowMessage('ok');
 
接受答案了.
 
后退
顶部