Jump to content


Photo

[Truco Delphi] Comparar cadenas usando comodines.


  • Please log in to reply
No replies to this topic

#1 sir.dev.a.lot

sir.dev.a.lot

    Advanced Member

  • Miembros
  • PipPipPip
  • 545 posts
  • Location127.0.0.1

Posted 18 August 2016 - 06:10 AM

[Truco Delphi] Comparar cadenas usando comodines.

 

Descripcion:

permite la comparacion de dos cadenas usando comodines, asteriscos, etc, ver ejemplo de uso.


delphi
  1. function MatchStrings(source, pattern: String): Boolean;
  2. var
  3. pSource: Array [0..255] of Char;
  4. pPattern: Array [0..255] of Char;
  5.  
  6. function MatchPattern(element, pattern: PChar): Boolean;
  7.  
  8. function IsPatternWild(pattern: PChar): Boolean;
  9. var
  10. t: Integer;
  11. begin
  12. Result := StrScan(pattern,'*') <> nil;
  13. if not Result then Result := StrScan(pattern,'?') <> nil;
  14. end;
  15.  
  16. begin
  17. if 0 = StrComp(pattern,'*') then
  18. Result := True
  19. else if (element^ = Chr(0)) and (pattern^ <> Chr(0)) then
  20. Result := False
  21. else if element^ = Chr(0) then
  22. Result := True
  23. else begin
  24. case pattern^ of
  25. '*': if MatchPattern(element,@pattern[1]) then
  26. Result := True
  27. else
  28. Result := MatchPattern(@element[1],pattern);
  29. '?': Result := MatchPattern(@element[1],@pattern[1]);
  30. else
  31. if element^ = pattern^ then
  32. Result := MatchPattern(@element[1],@pattern[1])
  33. else
  34. Result := False;
  35. end;
  36. end;
  37. end;
  38.  
  39. begin
  40. StrPCopy(pSource,source);
  41. StrPCopy(pPattern,pattern);
  42. Result := MatchPattern(pSource,pPattern);
  43. end;

Ejemplo de Uso:


delphi
  1. if MatchStrings('Delphi Access','Delph*') then showmessage('Bien hecho!');

Saludos!


  • 2




IP.Board spam blocked by CleanTalk.