Fórum busca numa array #226464
16/04/2004
0
-----------------------------------------------------------------------------
Var
strtexto : array [0..5] of string;
texto : string;
begin
strtexto[0] := ´a´;
strtexto[1] := ´b´;
strtexto[2] := ´c´;
strtexto[3] := ´d´;
strtexto[4] := ´e´;
texto := ´d´;
if texto in [strtexto] then
ShowMessage(´sim´)
else
ShowMessage(´nao´);
-----------------------------------------------------------------------------
utilizar a opção in na array ou outra similar sem ter que utilizar um for[color=darkblue:77d9870a24][/color:77d9870a24]
Wellington Kledir
Curtir tópico
+ 0Posts
16/04/2004
Rômulo Barros
Var strtexto : array [0..5] of string; texto : string; C : Byte; begin texto := ´e´; strtexto[0] := ´a´; strtexto[1] := ´b´; strtexto[2] := ´c´; strtexto[3] := ´d´; strtexto[4] := ´e´; For C := 0 To Length(strtexto)Do Begin If(strtexto[C] = texto)Then Begin ShowMessage(´Achou...´); Exit; End; End; end;
:P
Gostei + 0
16/04/2004
Beppe
var
StrTexto: set of Char;
Ch: Char;
begin
StrTexto := [´a´..´e´]; // pode-se usar ranges qdo saum consecutivos
Ch := ´d´;
if Ch in StrTexto then
ShowMessage(´sim´);
end;
Sets são conjuntos de elementos ordinais(Integer, Char, enumerações, etc), por isso usa-se o Char invés de string.
Uma alternativa que não usa sets é construir um string com os padrões e usar a função Pos.
Gostei + 0
16/04/2004
Wellington Kledir
com palavras como fica....
Gostei + 0
16/04/2004
Aroldo Zanela
O exemplo que o Rômulo apresentou é suficiente, veja outra variação:
function StrCase(Selector: string; StrList: array of string): Integer; var I: Integer; begin Result := -1; for I := 0 to High(StrList) do begin if Selector = StrList[I] then begin Result := I; Break; end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin case StrCase(Edit1.Text, [´EP´, ´EM´, ´RS´]) of 0: ShowMessage(´1: Entrada de produtos´ ); 1: ShowMessage(´2: Entrada de materiais´); 2: ShowMessage(´3: Registro de servico´ ); else ShowMessage(´else: ´ + Edit1.Text); end; end;
Ou ainda:
var Palavras: TStringList; nPos: Integer; begin Palavras := TStringList.Create; Palavras.Add(´Um´); Palavras.Add(´Dois´); Palavras.Add(´Três´); Palavras.Add(´Quatro´); Palavras.Add(´Cinco´); Palavras.Sort; if Palavras.Find(´Dois´, nPos) then ShowMessage(´Encontrada´); Palavras.Free; end;
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)