preload

String/Text functions 2 (WinRunner, TSL)

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

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);
}

 


  • Leave a Reply

    * Required
    ** Your Email is never shared

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.