Função para Abreviar Nomes
Função muito útil para a abreviação de nomes.
Ex: José Carlos da Silva = José C. da Silva.
É necessário estar declarado SysUtils na seção uses.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| function AbreviaNomes(Nome: String): String;
var
Nomes: array of string;
i, n: integer;
begin
Result := Nome;
Nome := Trim(Nome);
Nome := Nome + #32;
i := Pos(#32, Nome);
if i > 0 then
begin
n := 0;
{ Separa os Nomes }
while i > 0 do
begin
SetLength(Nomes, Length(Nomes) + 1);
Nomes[n] := Copy(Nome, 1, i - 1);
Delete(Nome, 1, i);
i := Pos(#32, Nome);
inc(n);
end;
if n > 1 then
begin
{ Abreviar a partir do segundo nome, exceto o último. }
for i := 1 to n - 2 do
{ Contém mais de 3 letras? (ignorar de, da, das, do, dos, etc.) }
if Length(Nomes[i]) > 3 then
Nomes[i] := Nomes[i][1] + '.';
Result := '';
for i := 0 to n - 1 do
Result := Result + Trim(Nomes[i]) + #32;
end;
end;
end; |
function AbreviaNomes(Nome: String): String;
var
Nomes: array of string;
i, n: integer;
begin
Result := Nome;
Nome := Trim(Nome);
Nome := Nome + #32;
i := Pos(#32, Nome);
if i > 0 then
begin
n := 0;
{ Separa os Nomes }
while i > 0 do
begin
SetLength(Nomes, Length(Nomes) + 1);
Nomes[n] := Copy(Nome, 1, i - 1);
Delete(Nome, 1, i);
i := Pos(#32, Nome);
inc(n);
end;
if n > 1 then
begin
{ Abreviar a partir do segundo nome, exceto o último. }
for i := 1 to n - 2 do
{ Contém mais de 3 letras? (ignorar de, da, das, do, dos, etc.) }
if Length(Nomes[i]) > 3 then
Nomes[i] := Nomes[i][1] + '.';
Result := '';
for i := 0 to n - 1 do
Result := Result + Trim(Nomes[i]) + #32;
end;
end;
end;
Exemplo de uso
1
2
3
4
5
| procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(AbreviaNomes('Antônio Carlos da Silveira'));
ShowMessage(AbreviaNomes('Luiz Augusto Campos'));
end; |
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(AbreviaNomes('Antônio Carlos da Silveira'));
ShowMessage(AbreviaNomes('Luiz Augusto Campos'));
end;
Baseado no post: http://www.sourcetips.net/delphi/strings/rotina-para-encurtar-nomes-proprios
-
Anderson Tonini
-
4.312 views
- 0 comentários
- 12 de março de 2016
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo