Solved

Find value in comma separated string

Posted on 2011-10-27
346 Views
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.
0
Question by:Delphiwizard

LVL 9

Expert Comment

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;
0

LVL 9

Expert Comment

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;
0

LVL 32

Accepted Solution

You could use stringlists

eg

``````function StrContains(const ASource, ACompare: string; out value: string): Boolean;
var
S1, S2: TStringList;
I: Integer;
begin
Result := False;
S1 := TStringList.Create;
S2 := TStringList.Create;
try
S1.Delimiter := ',';
S2.Delimiter := ',';
S1.StrictDelimiter := True;
S2.StrictDelimiter := True;

S1.DelimitedText := ASource;
S2.DelimitedText := Acompare;
for I := 0 to S1.Count -1 do
begin
Result := S2.IndexOf(S1[I]) > 0;
if Result then
begin
Value := s1[I];
Break;
end;
end;
finally
S1.Free;
S2.Free;
end;
end;
``````
0

Author Closing Comment

Perfect thank you.
0

LVL 36

Expert Comment

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 ');
0

LVL 36

Expert Comment

ah ... didn't refresh after going to lunch :)
0

LVL 9

Expert Comment

Is it my method false?
0

Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

Featured Post

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!