Jump to content
Ian Branch

Is a number in a string of numbers??

Recommended Posts

Hi Team,

I need to be able to detect and return true or false, if an integer number is in a delimited string of numbers..

i.e.  If the integer is say 47, and the delimited number string is '12;94;128;3;127,147', then it would return False.

      If the integer is say 47, and the delimited number string is '12;94;128;3;127,47', then it would return True.

      If the integer is say 47, and the delimited number string is '12;147;128;3;127,47', then it would return True.

Your thoughts/suggestions appreciated.

 

Regards & TIA,

Ian

Share this post


Link to post

Is the list correct in your example the first numbers are delimited with semicolon while the last is with comma. The easiest way to type this for me is:

 

function ListContains(const PList: string; PNumber: integer): boolean;
var
  sl: TStringList;
begin
  sl:=TStringList.Create;
  try
    sl.Delimiter:=';';
    sl.DelimitedText:=StringReplace(PList, ',', ';', []);
    result:=sl.IndexOf(IntToStr(PNumber))<>-1;

  finally
    sl.Free;
  end;
end;

 

Share this post


Link to post

Hi Lajos,

7 minutes ago, Lajos Juhász said:

Is the list correct in your example

Interdigital Interface Difficulties.  a.k.a. Finger Fault - It should be ;   :-(

Thank you for the code, I will have at it.

 

Regards,

Ian

Share this post


Link to post

If you make use of some newer features of the RTL you can also write:

uses
  System.SysUtils, System.StrUtils;

function ListContains(const PList: string; PNumber: integer): boolean;
begin
  Result := MatchStr(PNumber.ToString, PList.Split([';', ',']));
end;

An advantage is the simple support for additional separators.

Share this post


Link to post

 

You did not specify any problems, so these are coming to my mind right now.

 

 

 

 

Edited by Attila Kovacs

Share this post


Link to post

Hi Team,

Thank you for your further suggestions.  I am now going with Uwe's suggestion.

Uwe - I like simple.

0x9000FFFF - regular expressions scare me, like pointers... ;-)

Atilla - Thank you for the pointers.

 

Regards & Tks again,

Ian

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×