String Replace function is a pretty important and powerful function available in Siebel eScript. Powerful because we can use Regular expressions to find any type of pattern and replace it with our desired value. As usual I will try to explain with the help of example :)

Requirement:

I was working on a integration through webservices. They were sending response as an XML String. That was something like:

<? xml version= ”1.0” ?>
<statuobject>
<status> Success</status>
<reason> Quote inserted successfully </reason>
</statusobject>

Now I had to extract the values from the string and see if it has failed or passed. In case if it failed I had to send the mail including the reason code.

I could have solved it with two approaches.

1. Convert it to XML Hierarchy and extract the values using XML function in DTE Script
2. Use “replace” function to extract the values.

I will discuss the replace strategy which I finally used.

Solution: 


The code that finally did it for me was like this

  str = objectIn.GetProperty(”StatusString”);
 var pattern = /(<[^>]+>)/g;

// to remove the xml declaration part <? xml version= ”1.0” ?>
  status_str = str.substring(67,str.length - 29);

//replace the tags with “|” symbol
  finalstring = status_str.replace(pattern, “|”);

This code searched for tags in the string and replaced it with “|” symbol which I split using split function and got a array with the values.

result_ary = finalstring.split(”|”); 

So getting value was as easy as var stat =  result_ary[1] and so on

in pattern the /g represent global search if you don’t use that it will just replace the first occurrence not all the occurrences.

Discussing regular expression is out of scope but will surely discuss replace function in detail in the next posts.

OkAvarageGoodVery GoodExcellent (No Ratings Yet)
Loading ... Loading ...

Related Posts