Click here to return to the KiXtart HelpDesk main page...

KiXtart Script Depot

Text Search for Substring in string

;
; ScriptNavn:  TestSearch.kix
; Lavet af:     Steve Iatropoulos
; Email :        siatro@ipex.de     
; Beskrivelse: Subsroutine to extract substrings based on delimiters passed in.
;                   Example of : Subroutines, Variable use.
;
;  Search for a substring within a string bound between two delimiters.
;  Eg retunrn 1234 in string w1111-1234-n (bound between - and -).
;
; Passed in parameters:
;	Set the following variables before invoking
; 	$string=string to search in
;	$from = where to start extracting from, ^ means start of string
; 	$to = where you want to stop extracting (or end of string, whichever comes first)
;	$repeat=At which nth occurence to start the search from
;               (useful if you have multiple delimiters and you dont want to start from the first occurence of $from)
; Returns the extracted substring in $retstr
;

$i = 1
$retstr = ""
$c = ""
$length = 0

$length = LEN($string)
;
; Scan string until the right occurence of '$from' is reached...
;
WHILE $i <= $length AND $repeat > 0 AND $from <> "^"
	$c = substr($string, $i, 1)
	$i = $i + 1
	IF $c = $from
		$repeat=$repeat - 1
		ENDIF
	LOOP

$c = substr($string, $i, 1)
WHILE $i <= $length AND $c <> $to
	$i = $i + 1
	$retstr = $retstr + $c
	$c = substr($string, $i, 1)
	LOOP