Stef Merlijn
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.
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.
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;
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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||'','',' ',''||:nee dle||'','' ) needle_in_hay from dual';
qry.parambyName('hay').AsS tring := '1,2,3,4,5,6';
qry.paramByName('needle'). AsString := '5';
qry.Open;
if qry.FieldByName('needle_in _hay').AsI nteger > 0 then
ShowMessage('Needle is in hay ');
like so:
oracle:
select instr(','||:hay||',',','||
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||'','','
qry.parambyName('hay').AsS
qry.paramByName('needle').
qry.Open;
if qry.FieldByName('needle_in
ShowMessage('Needle is in hay ');
ah ... didn't refresh after going to lunch :)
Is it my method false?
Table1.Filtered := False;
Table1.Filter := 'MyField LIKE '+QuotedStr('%,1,%')+'or MyField LIKE '+
QuotedStr('1,%')+'or MyField LIKE '+QuotedStr('%,1');
Table1.Filtered := True;