PDA

Ver la Versión Completa : Rellenar con 0....


buitrago
20-10-2003, 23:02:14
Neceito una function que me rellene con 0 un string

Esto es que yo tengo un string y necesito rellenar con 0 a la izq ese mismo estrig, según la cantidad de 0 que especifique.

Hay algo escvrito en Delphi o el mismo Delphi me puede ayudar ....

Gracias.

roman
20-10-2003, 23:26:33
Si lo que necesitas es escribir un número con tantos ceros a la izquierda como sean necesarios para alcanzar determinada longitud quizá te sirva esta función:


function LPad(N: Integer; Count: Integer): String;
begin
Result := Format('%.*d', [Count, N]);
end;


Por ejemplo:

LPad(84, 5) --> '00084'
LPad(84, 7) --> '0000084';

Para otros casos en que la cadena rellenar no sea un número me parece, y espero estar equivocado, que no hay una función de tipo Pad en Delphi pero podrías construirla tu mismo usando un ciclo for para anexar los ceros:


for I := 1 to Count do
S := '0' + S;


o bien, si tu versión de Delphi la trae, usar la función DupeString de la unidad StrUtils:


S := DupeString('0', Count) + S;


// Saludos

brandolin
21-10-2003, 14:49:35
Yo he ceado una funcion como esta, no se si esta obtimizada pero funciona.


Function LlenarCeros(Original: String; long: Integer): String;
var x : integer;
Temp : String;
begin
Temp := '';
For X := 1 to Long-Length(Original) do
Temp:= '0' + Temp;
LlenarCeros := Temp + Original;
end;

buitrago
20-11-2003, 00:09:37
Gracias

juanlaplata
03-11-2011, 16:34:07
Viejo o no el post, es para los proximos en llegar.
Checar la funcion StringOfChar de la unit System.
Saludos

thelibmx
17-11-2011, 18:19:08
Pues para las nuevas generaciones va :rolleyes:

function LPad(S: String; Ch: Char; Len: Integer): String;
begin
Result := StringOfChar(Ch, Len - Length(S)) + S;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
e2.text:=(LPad(e1.text,'0',10));
end;

oscarac
17-11-2011, 20:02:53
bueno quiza ya fue respondida la pregunta....
pero aqui les envio lo que yo hago


function Llenar(strValue: String; intNewWidth: Integer): String;
var intOldWidth, I: Integer;
begin
try
strValue := Trim (StrValue);
if strValue = '' then
strValue := '0';
if StrToInt(strValue) < 0 then
strValue := '0';
except
on EConvertError do strValue := '0';
end;
intOldWidth := Length(strValue);
if intOldWidth < intNewWidth then
for I := 1 to intNewWidth - intOldWidth do
strValue := '0' + strValue;
Result := strValue;
end;



para llamarlo


edtNumIngreso.Text := Llenar(edtNumIngreso.Text, 11);