Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search For a Character in a string

Status
Not open for further replies.

FerrieC

Programmer
Feb 13, 2004
6
GB
Hey guys,

I am trying to search round a string such as:
abcdefghi(10)[11]

I want to grab the value 11 to store in my table, so i want to stop when i come to the fist [ to grap the value i think???...

I am not sure how to do this, has anyone any ideas...

Thansk in advance for your help

ferriec
 
hi

Have you had a look at the Pos function?

OpenBracket := pos('[', mystr);
CloseBracket := pos(']', mystr);

numstr := copy (mystr, OpenBracket, CloseBracket-OpenBracket-1);

try
num := strtoint(numstr);
except
showmessage(' num not a valid number: '+numstr);
end;

BTW, are you sure that the string itself could not contain square brackets? eg ab[cd]ef(10)[11] If so, you'll have to be a bit more clever and maybe take the number out by going backwards thru the string (assuming it's always at the end).

lou
[penguin]

 
if the string has a "fixed" structure, then this is very simple, by fixed I mean that the value you are searching is allways contained between '[' and ']'.

then the code could look like this :

Code:
function MySearchFunction(SearchStr : string) : integer;

var I,J : integer;
    S   : string; 

begin
 Result:=0;
 if SearchStr <> '' then
  begin
   I:=Pos('[',SearchStr);
   J:=Pos(']',SearchStr);
   if (I > 0) and (J > 0) and (I < J) then
    begin
     S:=Copy(SearchStr,I+1,J-I-1);
     if S <> '' then 
       Result:=StrToIntDef(S,0);
    end;
  end;
end;

Cheers

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top