GARANTIR DESCONTO

Fórum Como colocar valores de um Memo dentro de um array #428994

19/11/2012

0

Gostaria de saber como colocar valores que estão dentro de um Memo em um Array?
Tipo
12+5
Gostaria que ficasse da seguinte forma dentro do array
array[0]:=1;
array[1]:=2;
array[2]:=+;
array[5]:=3;

Samuel Lopes

Samuel Lopes

Responder

Posts

19/11/2012

Samuel Lopes

Encontrei a função que faz o serviço

var
variavel:string;

variavel:=Memo1.Text; // Se tiver 12+5 dentro do memo

SetLength(variavel,10); // Esta função pega os valores da variável e colocar dentro de um array com 10 posições

ShowMessage(teste2[2]); // Vai imprimir 2
Responder

Gostei + 0

19/11/2012

Pjrm1470

Espero que ajude:

unit Unit2;

interface

uses
  Generics.Collections;

type
  TUsefulMethods = class
  public
    {
      Passa para parametro o texto e uma array como variável.
      Se retornar uma string, significa que ocorreu erro durante o processo
    }
    class function SetTextToArray(Text: WideString; var TheArray: array of string): string;
  end;

implementation

uses
  SysUtils;

{ TUsefulMethods }

class function TUsefulMethods.SetTextToArray(Text: WideString; var TheArray: Array of string): string;
var
  TextLength: Integer;
  I: Integer;
begin
  Result := '';

  // obtem o tamanho do texto
  TextLength := Length(Text);

  try
    // se a array ja tiver tamanho menor que o text, define o tamanho exato para a array dinamica.
    if (Length(TheArray) < TextLength) then
      SetLength(TheArray, TextLength);
  except
    on E: Exception do
      Result := 'Erro ao tentar definir novo tamanho à array.'+#13+'Erro: '+E.Message;
  end;

  // percorre cada char do texto
  try
    for I := 0 to TextLength - 1 do
      TheArray[I] := Text[I];
  except
    on E: Exception do
      Result := 'Erro ao tentar efetuar a conversão do tipo de dado!'+#13+'Erro: '+E.Message;
  end;
end;

end.
Responder

Gostei + 0

19/11/2012

Claudia Nogueira

Ou ainda assim:

Var
  arr : Array of string;
  i : Integer;
  sTexto : String;
begin
  sTexto := Trim(Memo1.Lines.Text);
  if Length(sTexto) <= 0 then
    Exit;
  SetLength(arr,Length(sTexto));
  for i := 1 to Length(sTexto) do
    arr[i-1] := sTexto[i];
end;


Depois se quiser percorrer o array:

  for i := 0 to Length(arr) -1 do
    ShowMessage(arr[i]);
Responder

Gostei + 0

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

Aceitar