PDA

View Full Version : How do I search for the end of a numeric string?


robmaggs
03-26-2007, 03:19 AM
Hiya, I'm trying to retrieve a string from a html file and in a very clumsy way have almost achieved what I want to do. However I'm stumped about how to sort the retrieved data. I need to clean up the numerical value after the "clientid=". The clientid field could have characters from 1 to n and is always followed by a character like " ' ; etc. How can I check through the numerical string until I reach the end number before the ;' " etc and then store the value?

I think the best way to do this would be to search initially for the string "clientid=" and then search for the variable numerical data following it ie 7875. I don't think using string.mid is the best way of doing this

I assume I would use a while loop and if the string was clientid=7875"; the program would check 7 then 8 then 7 etc until it finds the " and then will store only the numerical value as a new string. I'm struggling with this as I am new to programming and string manipulation but I really want to learn.

Here's what I have so far:

result = TextFile.ReadToString("C:\\htmlsource.txt");
clientid = String.Find(result, "clientid", 1, false);
clientid1 = String.Mid(result, clientid, 10);

Thankyou for taking the time to help me.

All the best Rob

CyberRBT
03-26-2007, 05:44 AM
Here's a generic function that will do what you're wanting. (This is similar to the way that I extract snippets from Web pages.)

function read_token_value_from_file (filepath, token)
-- returns "-1" if file does not exist
-- returns "" if the token or number does not exist
-- otherwise returns the clientid as a string
-- (use String.ToNumber on the result, if needed)

-- init default
local out_str = "-1";

-- read the file
if (File.DoesExist(filepath)) then
result = TextFile.ReadToString(filepath);
else
return (out_str);
end

-- find it
local nPos = String.Find(result, token);
out_str = ""; -- reset default
if (nPos > 0) then
-- extract text starting with the number
local temp_str = String.Left(result, nPos + String.Length(token) - 1);
temp_str = String.Replace(result, temp_str, "");
-- now, get the number
local counter = 1;
local asc = String.Asc(String.Mid(temp_str, counter, 1));
while ((asc >= 48) and (asc <= 57)) do
out_str = out_str .. String.Char(asc);
counter = counter + 1;
asc = String.Asc(String.Mid(temp_str, counter, 1));
end
end
result = "";
return (out_str);
end

-- test it...
this_clientID_str = read_token_value_from_file("AutoPlay\\Docs\\htmlsource.txt", "clientid=");
Dialog.Message("test this", this_clientID_str);