Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   "in" en sentencia "If" (https://www.clubdelphi.com/foros/showthread.php?t=79195)

cone220 14-06-2012 19:52:40

"in" en sentencia "If"
 
Estimados, cómo están?
tengo una consulta respecto de la aplicación de la sentencia IN dentro de un If.
Lo que necesito hacer es validar que una variable string se encuentre dentro de una lista de datos.

Para el ejemplo sería:

var vLista, vCadena: string;
begin
vLista := '(0,14,95,96,10,93,21,51,20,8,99,90)';
vCadena := '21';

if vCadena IN vLista then ....
end;

Gracias!!!!!

Casimiro Notevi 14-06-2012 20:08:58

Hola, recuerda poner los tags al código fuente, ejemplo:



Gracias :)


Por cierto, esa variable vLista es una string, igual que vCadena, no es ninguna lista.

Si quieres comprobar una cadena en otra cadena string puedes hacerlo con pos:

Cita:

function Pos ( const Needle, HayStack : string ) : Integer;
Description The Pos function finds the position of one string Needle within another HayStack.

If the string is not found, 0 is returned.

The search is case sensitive.
Y si quieres hacerlo realmente con una lista, el proceso es diferente, acláranos exactamente qué es lo que quieres hacer.

cone220 14-06-2012 20:21:03

Hola! Gracias por la respuesta.
Tomo nota de los tags :)

Voy a intentar explicar mejor lo que necesito. En SQL utilizo la sentencia IN para validar si un valor está dentro de una lista de valores. Y quiero utlizar algo similar a la sentencia IN de SQL.

Respecto de usar Pos, al no tener acotados los valores de la lista, debiera generar mas código para validar los strings. Ejemplo: Cadena= 0, lista=0,1,10,20,100, etc

luisgutierrezb 14-06-2012 21:48:31

Tal vez te sirva esta liga,

http://www.delphibasics.co.uk/RTL.asp?Name=Include

Saludos

Casimiro Notevi 14-06-2012 21:52:53

Quizás te interese más un tstringlist y comparar con cada uno de sus items.
O también un array "normal y corriente", mejor incluso.

Casimiro Notevi 14-06-2012 22:16:45

O esto con 'in':
Cita:

In
Example code : Determine whether a character is a letter begin
IsLetter('G'); // G is a letter of the (uppercase) alphabet
IsLetter('1'); // 1 is not a letter
end;

// Test if a character is a letter
procedure TForm1.IsLetter(Letter : char);
var
Alphabet : set of 'A'..'Z';

begin
Alphabet := ['A'..'Z'];

if Letter In Alphabet then
ShowMessage(Letter+' is in the alphabet')
else
ShowMessage(Letter+' is NOT in the alphabet');
end;
Show full unit code G is in the alphabet
1 is NOT in the alphabet


Ordinal expression in Set expression
Description The In keyword tests whether a value is in a set. It returns true if it is, false if not.
Notes Sets are limited to 256 different values. Each element is equated with the Integer values 0,1,2 ... 255

Integer sets map directly to these element values, and are therefore limited to a high value of 255.

However, you may compare a value greater than 255 with an Integer set. Delphi simply uses the lowest byte of the Integer. For example, a test value of 258 would have a lower byte value of 3.



cone220 14-06-2012 22:18:53

Gracias!!! Voy a probar lo que me indican y luego les cuento.


La franja horaria es GMT +2. Ahora son las 04:12:08.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi