String/Text functions 2 (WinRunner, TSL)
Parent page: Service Functions – String / WinRunner
Replace characters in a string
public function str_replace(in s, in sub1, in sub2) { auto loc, start, sub1_len; auto src, result; src = s; sub1_len = length(sub1); result = ""; while (1) { loc = index(src, sub1); if (loc == 0) break; else { result = result & substr(src, 1, loc - 1) & sub2; src = substr(src, loc + sub1_len); } } result = result & src; return(result); }
Replace pattern (regular expression) in a string
public function str_replace_ex(in s, in regex1, in sub2) { extern RLENGTH; # system variable auto loc, start; auto src, result; auto chr1, chr2; # infinite loop protection if (length(regex1) == 2) { chr1 = substr(regex1,1,1); chr2 = substr(regex1,2,1); if (chr2 == "*") regex1 = chr1 & chr1 & chr2; } src = s; result = ""; while (1) { loc = match(src, regex1); if (loc == 0) break; else { result = result & substr(src, 1, loc - 1) & sub2; src = substr(src, loc + RLENGTH); } } result = result & src; return(result); }