?
Solved

Find value in comma separated string

Posted on 2011-10-27
7
Medium Priority
?
353 Views
Last Modified: 2012-08-13
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
Comment
Question by:Stef Merlijn
7 Comments
 
LVL 9

Expert Comment

by:Mahdi78
ID: 37041453
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

by:Mahdi78
ID: 37041664
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

by:
Ephraim Wangoya earned 2000 total points
ID: 37041783

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;

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:Stef Merlijn
ID: 37043653
Perfect thank you.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 37043946
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 38

Expert Comment

by:Geert Gruwez
ID: 37043950
ah ... didn't refresh after going to lunch :)
0
 
LVL 9

Expert Comment

by:Mahdi78
ID: 37043991
Is it my method false?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

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

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

Join & Ask a Question