preload

String/Text functions 3 (WinRunner, TSL)

Posted by Albert Gareev on Mar 18, 2007 | Categories: Source codeText Data

Parent page: Service Functions – String / WinRunner

Remove extra space and tab characters within a string

public function str_shrink(in s, in separator) {
 auto res, i, c, StringArray[];
  
 res = str_trim(s);
 if (res == "")
  return(res);  

 res = str_replace(res, "\t", " ");
 c = split(s, StringArray, " ");
 if (c == 1)
  return(StringArray[1]);
 
 res = StringArray[1];
 for (i=2;i<=c;i++)
  res = res & separator & StringArray[i];
  
 return(res); 
}

Return position of a substring, looking from LEFT or RIGHT

public function str_index(in s, in sub_str, in dir) {
 auto loc, result = 0;
 auto src = s;
 auto sub_len = length(sub_str);
 
 if (dir == "LEFT")
  return(index(src, sub_str));
 if (dir != "RIGHT")
  return(0);

 while (loc = index(src, sub_str)) {
  result = result + loc + sub_len - 1;
  src = substr(src, loc + sub_len);
  }
 if (result > sub_len)
  result = result - sub_len + 1;
   
  
 return(result);
}

Matching strings

public function str_match_multy(in src_line, in regex_list) {
 auto dvRegex[];
 auto rc, i, c;
 c = split(regex_list, dvRegex, ",");
 if (c < 1) return(0);
 
 for (i=1;i<=c;i++) {
  rc = match(src_line, str_trim(dvRegex[i]));
  if (rc>0) return(i);
  }
  
 return(0); 
}
###
public function str_text_test(in s1, in s2, in regex_flag) {
 auto rc;
 
 if (toupper(regex_flag) == "REGEX")
  rc = (match(s1, s2) > 0);
 else 
  rc = (s1 == s2);
 
 return(rc); 
}

 


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.