GARANTIR DESCONTO

Fórum RPAD/LPAD em Delphi (era: Gostaria de saber se...) #346477

22/09/2007

0

Olá gostaria de saber se existem alguma função no delphi que faça o seguinte:

tenho uma string. e quero que essa string tenha tamanho x. Caso não tenha esse tamanho completa-se com espaços vazios por exemplo. Estilo a função LPAD e RPAD do Oracle onde:
LPAD(STRING,TAM,CARACTER_A_COMPLETAR) preenche com caracter_a_completar depois de string.
RPAD(STRING,TAM,CARACTER_A_COMPLETAR) preenche com caracter_a_completar antes de string.

Até mais.


Iniciante2007

Iniciante2007

Responder

Posts

24/09/2007

Paulo

function TFrmMenu.AjustaStr( str : string; tam: integer) : string; begin while length( str ) tam do str := str + ´ ´; if length(str) > tam then str := copy(str, 1, tam); result := str; end;

Ve se isto te ajuda. Na parte str := str + ´´, você coloca o quiser, neste caso tem espaço vazio, mas pode ser ´*´, ´@´, e etc....


Responder

Gostei + 0

24/09/2007

Alcantarus

Amigo,


Em minhas aplicacoes eu uso essa:

// Funcao TAM
function TfmPrincipal.Tam ( sField : string; iLength : integer; sChar: string ) : string;
begin
        while Length(sField) < iLength do
        begin
                sField := sField + sChar;
        end;
        Result := Copy(sField, 1, iLength);
end;


Abracos,

Alcantarus.


Responder

Gostei + 0

11/10/2007

Khundalini

Com a função Format() é possível fazer isso de forma mais prática e elegante. Vejam:

function RPad(const s: String; Pad: Integer): String;
begin
if (Trim(s) = EmptyStr) or (Pad = 0) then
Result := EmptyStr
else
Result := Format(´¬-*.*s´, [Pad, Pad, s]);
end;

function LPad(const s: String; Pad: Integer): String;
begin
if (Trim(s) = EmptyStr) or (Pad = 0) then
Result := EmptyStr
else
Result := Format(´¬*.*s´, [Pad, Pad, s]);
end;


Sds.,
Rubem Rocha
Manaus, AM


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar