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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching in a string

Status
Not open for further replies.

Idunnowhattopick

Technical User
Jun 26, 2006
4
AU
Help,
I am writing a small interface to a automation controller, it accepts http requests. Using the Indy IDHTTP client I am able to get the following response:-
<html>
<head><title>Servlet Example</title></head>
<body><pre>
swid=/Test/XBee/RF01
prop=AO01
type=integer
value=78
</pre></body></html>
What I am interested in is the 'value=78' - can anyone tell me how to extract the number out of this string? The format of the answer is always the same only the value changes.

Thanks,
Craig A
 
Hi!

It's so easy...
You can use Delete Procedure as below =>
/********************************************
procedure Delete(var S: string; Index, Count:Integer);
Removes a substring from a string.
/********************************************
var
s: string;
num: integer;

{ S='value=78' }

delete(s,1,6); {deletes 6 characters from the first of the string('value=') , s='78' }
num := strtoint(s); { num=78 }
/*********************************************

OSP
 
Thanks OSP,

You've started me in the right direction, the only issue I see is when I call the function from the idhttp client it returns everything in one go from <HTML> to </HTML> so I need to find the correct starting point (the line with 'value=') and then grab all of the number which could be 1 to 9999 (anything before the carriage return)and exclude anything after the carriage return.

I hope that makes sense, being a Air Conditioning Mechanic forced to turn computer programmer - I'm doing my best.....

Craig
 
you mean all the html code stores in one string variable and you want to extract 'value=xxxx' and then the number?
 
Exactly,

What I want to know is out of the whole big string with carriage returns etc, what is the number that is after value= ?

I thought it was probably something to do with the old MidString function in Basic from 20 years ago, but Ive looked at heaps of sites and books, and everyone wants to talk about OOP and Web services etc, nobody publishes anything on basic stuff anymore.

Thanks for the help,
Craig
 
Hey!

var
s: string; { main string }
ss: string; { temp string }
n: integer;
Number: integer;

n := pos('value=',s); { get the index val }
ss := copy(s,n,10); { ss='value=xxxx' }
{value=xxxx extracted to ss}

{by the way for example ss can be 'value=1000' or 'value=78 <' because copy function reads 10 chars from the first of 'value=' so if the number has 4 digits it's ok but if the number has less than 4 digits we may have some additional chars from the next line so we have to delete them}

n := pos(#13,ss); { index of Enter char at the end of the line (if there's no additional chars n will be 0 }
delete(ss,n,4); { if there's no additional chars the function returns the string with no changes}

{ Now we have the string 'value=xxxx'. to extract the number you can continue with the code I wrote above }

delete(ss,1,6);
Number := strtoint(ss); { The Answer }

OSP
 
Thanks OSP,

I'll try that tonight, but it looks good, and even I can understand it.

Thanks

Craig
 
Since the format is the same, you could grab the value using 1 line of code. Where str=the response...

Code:
iValue:=StrToInt(copy(str,pos('value=',str)+6,pos('</pre',str)-pos('value=',str)-6));


Ryan
 
I find one line solutions such as the one suggested by Ryan difficult to understand and maintain.

I would recommend writing a general purpose function that could be used again if other values are required from the HTML data in the future.

For example, the following function uses the TStringList values property.
Code:
function GetValue( data: string; tag: string ): string;
var
  stringlist: TStringList;
begin
  stringlist := TStringList.Create;
  try
    stringList.Text := data;
    result := stringList.Values[tag];
  finally
    stringList.Free;
  end;
end;
Assuming your HTML text is in a string called HTMLtext then GetValue is called by
Code:
  item := GetValue( HTMLtext, 'Value' );
If at some time you needed to get the value of swid (see original post) then you would code
Code:
  item := GetValue( HTMLtext, 'swid' );
Even better would be to write a new class based on the IdHTTP class so that you add a value property to the new class. Although this initially involves extra effort it should improve the readibility and maintainability of your code.

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top