Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   PosEx, mejora de la función Pos (https://www.clubdelphi.com/foros/showthread.php?t=80663)

dec 01-07-2006 00:21:33

PosEx, mejora de la función Pos
 
Esta funcion lo que hace es buscar en el texto especificado un patron, y devolver la posicion en la que se encontro, pero permite mas funcionalidad, al poder elegir buscar de atras para adelante, o de poder dar la cantidad de coincidencias que hay.

Me imagino que se la podria optimizar, y se le puede agregar que no sea CASE sensitive, y varias cosas mas que puede me ponga a hacer cuando las necesite.

Código Delphi [-]
(*

 Uso:  variable := PosEx(texto,patron,veces);

 texto : el texto en el que se va a bucar las coincidencias
 patron: el texo que se va a buscar para hacer coincidir
 veces : indica el tipo de busqueda;

  <0 la busquede se hace de atras para adelante

  0  devuelve la cantidad de coindicencias

  >0 la busquede se hace normal
     un numero cualquiera devuelve la posicion de la
     coincidencia con igual numero
     si no hay tal cantidad de coincidencias, devuelve 0

 Ejemplo: (texto := 'En una casa, muy linda...')
   PosEx(texto,' ',0)  devuelve 4
   PosEx(texto,'.',1)  devuelve 23
   PosEx(texto,'.',-1) devuelve 25
*)

function PosEx(const texto, patron : string; const veces : integer) : integer;
var vtexto, letra : string;
    vecesb, cointot, posicion, termina : integer;
    i, dir, posi, posi2 : integer;
begin
//Seteo de variables necesarias
vtexto := texto;
vecesb := veces;
if veces = -1 then vecesb := 1;
if veces < -1 then vecesb := (veces*veces)+veces;
cointot := 0;
posicion := 0;
termina := 0;

    //Algoritmo
    if veces >= 0 then begin
      i := 1;
        dir := 1;
        posi := 0;
        posi2 := 1;
    end
    else begin
      i := length(vtexto);
        dir := -1;
        posi := length(vtexto);
        posi2 := 0;
    end;
  while (length(vtexto) > 0) and (termina = 0) do begin
      letra := copy(vtexto,posi,1);
        delete(vtexto,posi+posi2,1);
    if (letra = patron) then begin
          inc(cointot,1);
      //porque -2*-2 = 4, 4+(-2) = 2 :)
            if (vecesb = cointot) then begin
              posicion := i;
                termina := 1;
            end;
        end;
        inc(i,dir);
        if veces < 0 then inc(posi,dir);
  end; //while length(linea) > 0

  if veces = 0 then
    posicion := cointot;

PosEx := posicion
end;

courtois 30-06-2007 09:43:31

Sería bueno nombrarla de otra forma, digo para no confundirla, ya que en StrUtils tenemos la funcion PosEx con los mismos parámetros, solo que el ultimo sirve para indicar a partir de donde se hace la busqueda

function PosEx(const SubStr, S: string; Offset: Cardinal = 1): Integer;

PosEx returns the index of SubStr in S, beginning the search at Offset. If Offset is 1 (default), PosEx is equivalent to Pos.

PosEx returns 0 if SubStr is not found, if Offset is greater than the length of S, or if Offset is less than 1.


La franja horaria es GMT +2. Ahora son las 00:35:16.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi