How do I search for the end of a numeric string?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • robmaggs
    Forum Member
    • Apr 2006
    • 16

    How do I search for the end of a numeric string?

    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
    Forum Member
    • Nov 2003
    • 155

    #2
    Try This

    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.)

    Code:
    function read_token_value_from_file (filepath, token)
    [COLOR="SeaGreen"]-- 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)[/COLOR]
    	
    	[COLOR="SeaGreen"]-- init default[/COLOR]
    	[COLOR="Blue"]local[/COLOR] out_str = "-1";
    
    	[COLOR="SeaGreen"]-- read the file[/COLOR]
    	[COLOR="Blue"]if[/COLOR] (File.DoesExist(filepath)) [COLOR="Blue"]then[/COLOR]
    		result = TextFile.ReadToString(filepath);
    	[COLOR="Blue"]else[/COLOR]
    		[COLOR="Blue"]return[/COLOR] (out_str);
    	[COLOR="Blue"]end[/COLOR]
    	
    	[COLOR="SeaGreen"]-- find it[/COLOR]
    	[COLOR="Blue"]local[/COLOR] nPos = String.Find(result, token);
    	out_str = ""; [COLOR="SeaGreen"]-- reset default[/COLOR]
    	[COLOR="Blue"]if[/COLOR] (nPos > 0) [COLOR="Blue"]then[/COLOR]
    		[COLOR="SeaGreen"]-- extract text starting with the number[/COLOR]
    		[COLOR="Blue"]local[/COLOR] temp_str = String.Left(result, nPos + String.Length(token) - 1);
    		temp_str = String.Replace(result, temp_str, "");
    		[COLOR="SeaGreen"]-- now, get the number[/COLOR]
    		[COLOR="Blue"]local[/COLOR] counter = 1;
    		[COLOR="Blue"]local[/COLOR] asc = String.Asc(String.Mid(temp_str, counter, 1));
    		[COLOR="Blue"]while[/COLOR] ((asc >= 48) [COLOR="Blue"]and[/COLOR] (asc <= 57)) [COLOR="Blue"]do[/COLOR]
    			out_str = out_str .. String.Char(asc);
    			counter = counter + 1;
    			asc = String.Asc(String.Mid(temp_str, counter, 1));
    		[COLOR="Blue"]end[/COLOR]
    	[COLOR="Blue"]end[/COLOR]
    	result = "";
    	[COLOR="Blue"]return[/COLOR] (out_str);
    [COLOR="Blue"]end[/COLOR]
    
    [COLOR="SeaGreen"]-- test it...[/COLOR]
    this_clientID_str = read_token_value_from_file("AutoPlay\\Docs\\htmlsource.txt", "clientid=");
    Dialog.Message("test this", this_clientID_str);
    Last edited by CyberRBT; 03-26-2007, 04:55 AM.

    Comment

    Working...
    X