R. E. Cook
Home 
Contact Us 

Dr. Cook
Resume 

RunningShoes Library for WinRunner 
RunningShoes 

Custom WinRunner Functions 
and Utilities,
to Your Specs
Custom 

A Random Collection of WinRunner Solutions
Solutions 

Creating DLLs for WinRunner 
"DLLs Demystified" 
(Mercury Interactive's Worldwide Users Conference 2000)

R. E. Cook Consulting

 


Capturing Substrings

Functions: 
SubstrAfter(), SubstrBefore(), SubstrBetween(), 
FindLast(), FindWhich(), FindFromRight(), 
IndexFromRight()


In WinRunner without RunningShoes, capturing a substring from a source string can be a breeze or a nightmare, depending on how much we know about the string. 

If we know the exact position and length of a substring within the source string, WinRunner makes it easy to capture the substring:

# Capture the last name. It starts in position 21, and is 20 characters long.
last_name = substr(source, 21, 20);


Unknown position, delimited

RunningShoes contains several functions designed to make it easy to capture substrings when the source string is not so rigidly arranged. If we know what characters come before and after the desired substring, we can find the substring by finding those characters that delimit the substring. 


Unique delimiters

If the delimiter characters are unique, we can find the substring easily using RunningShoes's Substr__() functions. 

For example, we are looking through an HTML file for the page title. We know that in HTML, the title is between the tags "<TITLE>" and "</TITLE>", so we can use those tags as delimiters:

# Define the delimiters
delim_1 = "<TITLE>";
delim_2 = "</TITLE>";

# With RunningShoes, we capture the title substring with a single line: 
title = SubstrBetween(source_code, delim_1, delim_2);

Without RunningShoes, we can still find the substring in a roundabout way. Here's the code we saved by using RunningShoes.


Two identical delimiters

If the substring is delimited by the same characters, SubstrBetween() will still find it as long as those delimiters do not appear elsewhere in the string. 

Example: the id_string contains "first name, last name, age".

# The last name is between commas, no other commas appear in the string:
last_name = SubstrBetween(id_string, ",", ",");


Multiple non-unique delimiters

If the substring is delimited by characters that appear elsewhere in the string, SubstrBetween() won't work. We must first find which delimiters we want. Use the various Find__() functions to determine the position of the substring. This is a case where RunningShoes really makes our lives (or at least our scripting) easier.

Example: id_string contains "first name, middle name, last name, age, sex"
The last name is between the 2nd and 3rd commas (comma followed by space).

# Define delimiter (comma space) and determine its length.
delimiter = ", ";
len_delim = length(delimiter);

# Find the 2nd comma.
start = FindWhich(id_string, delimiter, 2);

# Move the starting position past the comma.
start += len_delim;

# Find the 3rd comma.
end = FindWhich(id_string, delimiter, 3);

# Capture the last name between the 2nd and 3rd commas.
last_name = substr(id_string, start, end - start);

Without RunningShoes, it's a challenge. Here's 24 lines of code we would have to write, debug, and maintain to accomplish the same thing as the 6 lines above. (It's in a text file to avoid browser format mangling.)


Updated 17 January 2005



Top of  This Page | R. E. Cook Home Page

Copyright R. E. Cook Consulting, 2000 - 2011.
All rights reserved.

The site is designed to look best when viewed with Internet Explorer 5.0 - 8.0. Results with other browsers will probably deviate from what is intended.

NOTE: This website refers to registered trademarks and service marks that are owned by other companies, including Mercury Interactive, WinRunner, TestDirector, TSL, Microsoft, Windows.