strings

Delphi

07/02/2006

Oi pessoal,


[u:a2c8ab0423]Tenho a seguinte string:[/u:a2c8ab0423]

strig := ´um.dois.tres´

[u:a2c8ab0423]E queria passar o array_string, de modo a que o conteudo ficasse:[/u:a2c8ab0423]

array_string[0]:=´um´;
array_string[1]:=´dois´;
array_string[2]:=´tres´;

Sendo strig de tamanho indefinido, ou seja, pode ter X ponto.

Resumindo queria que sempre que encontrasse um ponto array_string incrementava o indice.


Um bocado confuso mas espero ter-me explicado bem

Agradecia a ajuda


Nilpedro

Nilpedro

Curtidas 0

Respostas

José Henrique

José Henrique

07/02/2006

Use TStringList

var
  strlTeste : TStringList;
  i, n : integer;
begin
   try
     strlTeste := TStringList.Create;
     strlTeste.Delimiter := ´.´;   este é o separador escolhido pelo você
     strlTeste.DelimitedText := ´um.dois.tres´
     n := strlTeste.Count;
     for i := 1 to strlTeste.Count;
        ShowMessage(strlTeste[i]);
   finally
     strlTeste.Release;
   end;
end;



GOSTEI 0
José Henrique

José Henrique

07/02/2006

Não precisa da variável n
var 
  strlTeste : TStringList; 
  i : integer; 
begin 
   try 
     strlTeste := TStringList.Create; 
     strlTeste.Delimiter := ´.´;   //este é o separador escolhido pelo você 
     strlTeste.DelimitedText := ´um.dois.tres´ ; 
     for i := 1 to strlTeste.Count; 
        ShowMessage(strlTeste[i]); 
   finally 
     strlTeste.Release; 
   end; 
end; 
 



GOSTEI 0
Nilpedro

Nilpedro

07/02/2006

Agradeço a ajuda,


Mas estou trabalhando com Dephi 5 e este comando nao dá. Se tiverem outra sujestao :wink:


GOSTEI 0
Aroldo Zanela

Aroldo Zanela

07/02/2006

Colega,

Acredito que funcione em todas as versões:

var LineData: String;
    I, X: Integer;
    FieldsData: TStringList;
begin
  FieldsData  := TStringList.Create;
  LineData    := ´um.dois.tres´;
  try
    while Length(LineData)>0 do
    begin
      X := Pos(´.´,LineData);
      if X>0 then
      begin
        FieldsData.Add(Copy(LineData, 1, X-1));
        LineData := Copy(LineData, X+1, Length(LineData)-x);
      end else
      begin
        FieldsData.Add(LineData);
        LineData := ´´;
      end;
    end;
  finally
    FieldsData.Free;
  end;



GOSTEI 0
Michael

Michael

07/02/2006

Dica: Encapsule a dica do colega [b:6add323a92]Aroldo [/b:6add323a92]em uma procedure:

procedure ParseString(S, Delimiter: string; Strings: TStrings);
var
  X: Integer;
begin
  while Length(S) > 0 do
  begin
    X := Pos(Delimiter, S);
    if X > 0 then
    begin
      Strings.Add(Copy(S, 1, X - 1));
      S := Copy(S, X + 1, Length(S) - X);
    end 
    else
    begin
      Strings.Add(S);
      S := ´´;
    end;
  end;
end;


[]´s
[]´s


GOSTEI 0
POSTAR