Página 1 de 1
[Delphi] Function sTrim
Publicado: 01 Jul 2010, 17:04
por The Swash
Código: Seleccionar todo
function sTrim(lpStr:string):String;
var
i: Integer;
begin
If Length(lpStr) > 0 Then
begin
for i := 1 to Length(lpStr) do begin
If Copy(lpStr,i,1) = ' ' Then begin
If (Copy(lpStr,i,1) = ' ') And (Copy(lpStr,i + 1,1) <> ' ') And (Copy(lpStr,i - 1,1) <> ' ')Then Result:= Result + Copy(lpStr,i,1);
Result:= Result + '';
end else
Result:= Result + Copy(lpStr,i,1)
end;
end;
end;
Bueno amigos, probando me salió esta función y te ahorra su dependencia original [Trim -> SysUtils.pas], espero les sirva, cuando recomendación o critica la agradezco, como Siempre mis agradecimientos a
Thor!
Re: [Delphi] Function sTrim
Publicado: 01 Jul 2010, 21:27
por p0is0n-123
Realmente vos tenes facilidad para aprender los lenguajes bro
Gracias man,muy interesante
Salu2
Re: [Delphi] Function sTrim
Publicado: 01 Jul 2010, 21:36
por The Swash
Código: Seleccionar todo
function sTrim(lpStr:String):String;
var
i: Integer;
begin
If Length(lpStr) > 0 Then
begin
for i := 1 to Length(lpStr) do begin
If lpStr[i] = ' ' Then begin
If (lpStr[i] = ' ') And (lpStr[i+1] <> ' ') And (lpStr[i-1] <> ' ')Then Result:= Result + lpStr[i];
end else
Result:= Result + lpStr[i]
end;
end;
end;
Agradezco a Psymera por darme recomendaciones y pues esta función es totalmente independiente, usa el array of char de una string y Funciona mejor! Espero lo disfruten!
Re: [Delphi] Function sTrim
Publicado: 01 Jul 2010, 23:43
por The Swash
haha la tercera es la vencida y pues, despues de que psymera me reporto un BUG increible donde no funcionaba si habia mas de 1 espacio intermedio, reconstrui la Funcion reitero mis Agradecimientos a Thor y Psymera les dejo la nueva funcion 100% Independiente:
Código: Seleccionar todo
function sTrim(lpStr:String):String;
var
i: Integer;
nTemp: Integer;
nTemp2: Integer;
begin
If Length(lpStr) > 0 Then
begin
for i := 1 to Length(lpStr) do
begin
If lpStr[i] <> ' ' Then
begin
nTemp := i;
break;
end;
end;
for i := Length(lpStr) downto 1 do
begin
If lpStr[i] <> ' ' Then
begin
nTemp2:=i;
break;
end;
end;
for i := nTemp to nTemp2 do
begin
Result:=Result + lpStr[i]
end;
end;
end;
Re: [Delphi] Function sTrim
Publicado: 02 Jul 2010, 00:42
por R-007
te quedó muy bien el último code, pero aquí te resumo los 2 bucles en 1 sólo usando booleans en vez de breaks:
Código: Seleccionar todo
function s1Trim(lpStr:String):String;
var
i: Integer;
j: Integer;
nTemp1: Integer;
nTemp2: Integer;
find1: boolean;
find2: boolean;
begin
If Length(lpStr) > 0 Then
begin
nTemp1:= 0;
nTemp2:= 0;
find1:= false;
find2:= false;
i:= 1;
j:= Length(lpStr);
while((i<=Length(lpStr))and(not(find1 and find2))) do
begin
If ((lpStr[i] <> ' ')and(not find1)) Then
begin
nTemp1 := i;
find1 := true;
end;
If ((lpStr[j] <> ' ')and(not find2)) Then
begin
nTemp2 := j;
find2 := true;
end;
i:= i+1;
j:= j-1;
end;
for i := nTemp1 to nTemp2 do
begin
Result:=Result + lpStr[i]
end;
end;
end;
Por cierto, te faltaba la inicialización de nTemp1 y nTemp2 puesto que si no hay que el string no contiene ningún espacio, el bucle for i:= nTemp1 to nTemp2 do, puede ocasionar problemas.
Un saludo!
Re: [Delphi] Function sTrim
Publicado: 02 Jul 2010, 14:13
por The Swash
R-007 Se ve que manejas delphi muy bien, te agradesco tus mejoras y comentarios! Salu2!
Re: [Delphi] Function sTrim
Publicado: 02 Jul 2010, 21:27
por R-007
jejej bueno yo realmente me manejo con soltura en Delphi, C++ y Java.. aunque también se lenguajes de scripting y php.
de momento en Delphi estoy mirando muchos códigos todos los días para ver si puedo hacer algo realmente interesante jeje
yo encantado de poder ayudaros a mejorar! aunque sea un punto y coma que se haya escapado por ahí jejeje
Un saludo!