ExtractStrings: Você conhece? Separar string por um delimitador
Buenas, vou mostrar uma funções do delphi que normalmente quebram muitos galhos, vou fazer isso pois tem muitos que não conhecem (Inclusive eu) e, por desconhece-las, acabamos “quebrando a cabeça” pra fazer algo “parecido”. Utilizando “Pos” e “Copy” por exemplo.
Função Nativa do Delphi: ExtractString:
function ExtractStrings(Separators, WhiteSpace: TSysCharSet;
Content: PChar; Strings: TStrings): Integer;
Separator – É um array onde você pode definir vários separadores
WhiteSpace – É um Array onde você define os caracteres que devem ser Ignorados quando ocorrerem no início da String.
Content – É a String de onde se deseja extrais as substrings
Substrings - A Função retorna o número de extraídas.
Exemplo: Você tem uma lista que tem diversos telefones separados por “;”. E precisa separar as colunas para melhor visualização do seu usuário, e como fazer isso?
procedure TFExtractStrings.BtnOkClick(Sender: TObject);
var
wLinha, wTelefone: String;
wLista: TStringList;
I: Integer;
begin
wLinha := edLinha.Text;
wLista := TStringList.Create;
try
wLista.Clear;
ExtractStrings([';'], [], PChar(wLinha), wLista);
for I := 0 to wLista.Count - 1 do begin
if wLista.Count >= 1 then
wTelefone := wLista[I];
if Trim(wTelefone) <> EmptyStr then
ShowMessage(wTelefone);
end;
//Ou todos na mesma mensagem
if wLista.Count > 0 then
ShowMessage(wLista.Text);
finally
wLista.Free;
end;
end;
Exemplo: Download do exemplo
excelente dica, só precisei adaptar a linha
ResponderExcluirExtractStrings([';'], [], PChar(guias), ListaGuias);
guias recebe retorno de uma função com uma lista separada
por ";" e da stringlist jogo num listbox.
muito show. valeu, suas dicas sempre me salvam.
:D
Excluir