Formatar string
Bom dia,
Tentei usar a opção de procura para ver se já existia um tópico relacionado, porém não está funcionando. A pergunta é: como fazer para que a string 05827907000144 saia 05.827.907/0001-44 em uma saida para impressora usando o writeln.
Ex: writeln(impressora, [color=red:d3e777f143]formatstring[/color:d3e777f143](´#../-´,cgc);
procurei uma função parecida com formatstring e não achei. Existe uma que faça está formatação?
Reuber
Tentei usar a opção de procura para ver se já existia um tópico relacionado, porém não está funcionando. A pergunta é: como fazer para que a string 05827907000144 saia 05.827.907/0001-44 em uma saida para impressora usando o writeln.
Ex: writeln(impressora, [color=red:d3e777f143]formatstring[/color:d3e777f143](´#../-´,cgc);
procurei uma função parecida com formatstring e não achei. Existe uma que faça está formatação?
Reuber
Jubrovolski
Curtidas 0
Respostas
Rômulo Barros
15/12/2004
Se a sua string possuir sempre 14 caracteres, faça:
:wink: :wink:
procedure TForm1.Button1Click(Sender: TObject); Var Teste : String; begin Teste := ´05827907000144´; Insert(´.´,Teste,3); Insert(´.´,Teste,7); Insert(´/´,Teste,11); Insert(´-´,Teste,16); ShowMessage(Teste); end;
:wink: :wink:
GOSTEI 0
Jubrovolski
15/12/2004
Obrigado pela resposta, porém queria uma função genérica que formatasse qualquer string para qualquer mascara.
GOSTEI 0
Inutaishou
15/12/2004
Colega, sendo isso que você passou, você pode usar a FormatFloat, porém, não funcionaria com Strings que contivessem letras...
[]´s,
[]´s,
GOSTEI 0
Rômulo Barros
15/12/2004
Obrigado pela resposta, porém queria uma função genérica que formatasse qualquer string para qualquer mascara.
Utilize o componente MaskEdit. Click sobre a propriedade EditMask e pressione F1. Veja:
Represents the mask that validates and formats user input. Unit MaskUtils type TEditMask = type string; Description TEditMask is a string that consists of three fields with semicolons separating them. The first part of the mask is the mask itself. The second part is the character that determines whether the literal characters of a mask are saved as part of the data. The third part of the mask is the character used to represent unentered characters in the mask. These are the special characters used in the first field of the mask: CharacterMeaning in mask !If a ! character appears in the mask, optional characters are represented in the text as leading blanks. If a ! character is not present, optional characters are represented in the text as trailing blanks. >If a > character appears in the mask, all characters that follow are in uppercase until the end of the mask or until a < character is encountered. <If a < character appears in the mask, all characters that follow are in lowercase until the end of the mask or until a > character is encountered. <>If these two characters appear together in a mask, no case checking is done and the data is formatted with the case the user uses to enter the data. \The character that follows a \ character is a literal character. Use this character to use any of the mask special characters as a literal in the data. LThe L character requires an alphabetic character only in this position. For the US, this is A-Z, a-z. lThe l character permits only an alphabetic character in this position, but doesn´t require it. AThe A character requires an alphanumeric character only in this position. For the US, this is A-Z, a-z, 0-9. aThe a character permits an alphanumeric character in this position, but doesn´t require it. CThe C character requires an arbitrary character in this position. cThe c character permits an arbitrary character in this position, but doesn´t require it. 0The 0 character requires a numeric character only in this position. 9The 9 character permits a numeric character in this position, but doesn´t require it. #The character permits a numeric character or a plus or minus sign in this position, but doesn´t require it. :The : character is used to separate hours, minutes, and seconds in times. If the character that separates hours, minutes, and seconds is different in the regional settings of the Control Panel utility on your computer system, that character is used instead. /The / character is used to separate months, days, and years in dates. If the character that separates months, days, and years is different in the regional settings of the Control Panel utility on your computer system, that character is used instead. ;The ; character is used to separate the three fields of the mask. _The _ character automatically inserts spaces into the text. When the user enters characters in the field, the cursor skips the _ character. Any character that does not appear in the preceding table can appear in the first part of the mask as a literal character. Literal characters must be matched exactly in the edit control. They are inserted automatically, and the cursor skips over them during editing. The special mask characters can also appear as literal characters if preceded by a backslash character (\). The second field of the mask is a single character that indicates whether literal characters from the mask should be included as part of the text for the edit control. For example, the mask for a telephone number with area code could be the following string: (000)_000-0000;0;* The 0 in the second field indicates that the Text property for the edit control would consist of the 10 digits that were entered, rather than the 14 characters that make up the telephone number as it appears in the edit control. A 0 in the second field indicates that literals should not be included, any other character indicates that they should be included. The character that indicates whether literals should be included can be changed in the Edit Mask property editor, or programmatically by changing the MaskNoSave typed constant. The third field of the mask is the character that appears in the edit control for blanks (characters that have not been entered). By default, this is the same as the character that stands for literal spaces. The two characters appear the same in an edit window. However, when a user edits the text in a masked edit control, the cursor selects each blank character in turn, and skips over the space character. Note:When working with multibyte character sets, such as Japanese Shift-JIS, each special mask character represents a single byte. To specify multi-byte characters using the L, l, A, a, C, or c specifiers, the mask characters must be duplicated as well. For example, LL would represent two single-byte alphabetic characters or a one double-byte character. Only single-byte literal characters are supported.
GOSTEI 0
Jubrovolski
15/12/2004
Devido ao fato de não ter encontrado a função da qual precisava, resolvi pensar um pouco e desenvolver uma para as minhas necessidades. Quem quiser tai o código:
function formatstring(texto: string; mascara: string): string;
var matrizn: array[1..10] of Integer; // coloquei no máximo de 10 caracteres de mascara
matrizs: array[1..10] of string; // estas duas matrizes quando a posição e o caracter da mascara
I: Integer;
R: Integer;
retorno: string;
vmascara: string;
begin
vmascara:=´./-:´; // aqui informo os caracteres que são usados nas mascaras
r:=1;
for I := 1 to length(mascara) do // Iterate
begin
retorno:=copy(mascara,i,1);
if pos(retorno,vmascara) <> 0 then
begin
matrizn[R]:=I;
matrizs[R]:=retorno;
r:=r+1;
end;
end; // for
for I := 1 to r-1 do // Iterate
begin
if i = 1 then
insert(matrizs[i],texto,matrizn[i])
else
insert(matrizs[i],texto,matrizn[i]);
end; // for
result:= texto;
end; // final do código
Exemplo de uso
procedure TForm1.Button1Click(Sender: TObject);
Var
Teste : String;
begin
Teste := ´05827907000144´;
label1.caption := formatstring(Teste,´#../-´);
Teste := ´14780390´;
label2.caption := formatstring(Teste,´.-´);
Teste := ´1520´;
label3.caption := formatstring(Teste,´:´);
end;
function formatstring(texto: string; mascara: string): string;
var matrizn: array[1..10] of Integer; // coloquei no máximo de 10 caracteres de mascara
matrizs: array[1..10] of string; // estas duas matrizes quando a posição e o caracter da mascara
I: Integer;
R: Integer;
retorno: string;
vmascara: string;
begin
vmascara:=´./-:´; // aqui informo os caracteres que são usados nas mascaras
r:=1;
for I := 1 to length(mascara) do // Iterate
begin
retorno:=copy(mascara,i,1);
if pos(retorno,vmascara) <> 0 then
begin
matrizn[R]:=I;
matrizs[R]:=retorno;
r:=r+1;
end;
end; // for
for I := 1 to r-1 do // Iterate
begin
if i = 1 then
insert(matrizs[i],texto,matrizn[i])
else
insert(matrizs[i],texto,matrizn[i]);
end; // for
result:= texto;
end; // final do código
Exemplo de uso
procedure TForm1.Button1Click(Sender: TObject);
Var
Teste : String;
begin
Teste := ´05827907000144´;
label1.caption := formatstring(Teste,´#../-´);
Teste := ´14780390´;
label2.caption := formatstring(Teste,´.-´);
Teste := ´1520´;
label3.caption := formatstring(Teste,´:´);
end;
GOSTEI 0
Renato_sp
15/12/2004
Sua function ficou legal mas o delphi tem uma function que faria o que vc quer sem precisar montar function propria.. aqui vai ela
var
Cnpj:String;
begin
Cnpj:=Edit1.text;
Edit1.text:=FormatMaskText(´00.000.000/0000-00;0´,Cnpj);
end;
Ai neste exemplo ele formata a string, o interessante é que a formatacao vale tanto para numeros como para letras se cnpj tiver valor numero ficaria mais ou menos assim
10.120.130/0001-40
ou
aa.bbb.ccc/dddd-ee
Espero que tenha ajudado...
Ate mais
Renato / SP
var
Cnpj:String;
begin
Cnpj:=Edit1.text;
Edit1.text:=FormatMaskText(´00.000.000/0000-00;0´,Cnpj);
end;
Ai neste exemplo ele formata a string, o interessante é que a formatacao vale tanto para numeros como para letras se cnpj tiver valor numero ficaria mais ou menos assim
10.120.130/0001-40
ou
aa.bbb.ccc/dddd-ee
Espero que tenha ajudado...
Ate mais
Renato / SP
GOSTEI 0
Rômulo Barros
15/12/2004
Sua function ficou legal mas o delphi tem uma function que faria o que vc quer sem precisar montar function propria.. aqui vai ela
var
Cnpj:String;
begin
Cnpj:=Edit1.text;
Edit1.text:=FormatMaskText(´00.000.000/0000-00;0´,Cnpj);
end;
Ai neste exemplo ele formata a string, o interessante é que a formatacao vale tanto para numeros como para letras se cnpj tiver valor numero ficaria mais ou menos assim
10.120.130/0001-40
ou
aa.bbb.ccc/dddd-ee
Espero que tenha ajudado...
Ate mais
Renato / SP
:arrow: [u:65053cc29d][color=green:65053cc29d][b:65053cc29d]Taí... mais uma que eu não sabia e aprendi !!!! [/b:65053cc29d][/color:65053cc29d][/u:65053cc29d] :wink:
GOSTEI 0