Link to home
Start Free TrialLog in
Avatar of Stef Merlijn
Stef MerlijnFlag for Netherlands

asked on

Find value in comma separated string

Hi,

I need to determine if StringA contains an exact match for StringB.
Please be aware that the value of StringB must be matched with a value in StringA based on the whole value between the comma's.

Example 1:
StringA = '0,4,5,10';
StringB = '1';
NO MATCH as value '1' can't be found in StringA (as '10' is not an exact match).

Example 2:
StringA = '0,1,4,5,10';
StringB = '1';
EXACT MATCH as value '1' can be found in StringA.
Avatar of Mahdi78
Mahdi78
Flag of Algeria image

Use Filter of dataset like the follwoing

Table1.Filtered := False;
Table1.Filter := 'MyField LIKE '+QuotedStr('%,1,%')+'or MyField LIKE '+
QuotedStr('1,%')+'or MyField LIKE '+QuotedStr('%,1');
Table1.Filtered := True;
Or if you want to find text use the this function

function FindMyStr(StringB: string): boolean;
begin
Result := (Pos(',1,', StringB) > 0) or (Pos('1,', StringB) = 1)
or (Pos(',1', StringB) = Length(StringB) - 1);
end;
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stef Merlijn

ASKER

Perfect thank you.
if you want to do this on a database using parameters
like so:

oracle:
select instr(','||:hay||',',','||:needle||',') needle_in_hay from dual;
sample:
:hay = '1,2,3,4,5'
:needle = '4'

returns the position of the needle in the hay or 0 if it's not found

qry.Sql.text := 'select instr('',''||:hay||'','','',''||:needle||'','') needle_in_hay from dual';
qry.parambyName('hay').AsString := '1,2,3,4,5,6';
qry.paramByName('needle').AsString := '5';
qry.Open;
if qry.FieldByName('needle_in_hay').AsInteger > 0 then
  ShowMessage('Needle is in hay ');
ah ... didn't refresh after going to lunch :)
Is it my method false?