{"id":1569,"date":"2012-05-06T04:08:38","date_gmt":"2012-05-05T22:38:38","guid":{"rendered":"http:\/\/www.automationrepository.com\/?p=1569"},"modified":"2012-05-06T04:09:04","modified_gmt":"2012-05-05T22:39:04","slug":"vbscript-string-manipulation-functions-with-examples-in-qtp","status":"publish","type":"post","link":"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/","title":{"rendered":"Common string manipulation functions that you would be using almost daily in QTP"},"content":{"rendered":"<div style=\"text-align: justify; font-family: Verdana;\">\n<p>This article (and few more upcoming articles on VBScript Function Reference) is an extension of the <a title=\"QTP VBScript Tutorials\" href=\"http:\/\/www.automationrepository.com\/category\/qtp-and-vbscript-tutorials\/\" target=\"_blank\">QTP VBScript Tutorial<\/a> series that we had started sometime back. In this article, you would see some of the important and common VBScript string manipulation functions that you would most likely be using almost daily while working with QTP.<!--more--> We will explain each of these functions in depth using examples and code snippets.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">NOTE:<\/span> This article is primarily targeted towards QTP beginners who have just started their journey with QTP and are not aware about the various inbuilt QTP and VBScript functions available for their use. If you are a QTP\/VBScript expert or have spent good amount of time on the tool, you would most probably be aware of all these functions. So you can skip this article or can go through it as a quick refresher.<br \/>\n<\/strong><br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>Why this article?<\/h3>\n<p>There is a reason why this article is here and why it is specifically targeted towards QTP beginners. When I started my journey with Test Automation and QTP, I started working on a project with very little exposure to QTP. It was basically a &#8220;learn while you do&#8221; sort of job (of course I had experienced people in my team helping me in case I got stuck somewhere). This was the time when I had little practical exposure of QTP and VBScript. Many a times I faced situations where I had to write some small flow and I would actually start writing a function for the same without knowing that there already exists an in-built function for that. I would be later told by my team during code reviews (or if I got stuck somewhere) that the logic I have written can be done in a single line using in-built functions.<\/p>\n<p>If you are beginning to learn QTP, I&#8217;m sure that you would also be facing similar sort of situations. This article (and the upcoming series on VBScript function reference) is an attempt to help you understand and have an idea about the different inbuilt functions that you would be using very frequently while scripting. <strong>Moreover, rather than just presenting (and explaining) this list of common functions, we will try to come up with appropriate real life scenarios where you will be using these functions.<\/strong> This would help you understand these concepts and their application in a better manner.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>Classification of VBScript Function Library<\/h3>\n<p>For the ease of understanding, we will divide the entire VBScript function library that we use in QTP into following four types &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"QTP VBScript Function Library - String Manipulation Functions\" src=\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-vbscript-function-library.png\" alt=\"QTP VBScript Function Library - String Manipulation Functions\" width=\"497\" height=\"210\" \/><\/p>\n<p>Let&#8217;s now start with VBScript string manipulation functions that we use in QTP.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>Len Function<\/h3>\n<p><strong>Purpose: <\/strong>This function can be used in QTP to find the length of a string. That is, this VBScript function finds out the number of characters present in a string.<\/p>\n<p><strong>Syntax: <\/strong>Len(string)<br \/>\nWith this function, you can directly pass a string as an argument or you can pass a variable also.<\/p>\n<p><strong>Example: <\/strong>Let us suppose that you want to <strong><a title=\"Create Random Number in QTP\" href=\"http:\/\/www.automationrepository.com\/2011\/10\/generate-random-numbers-in-qtp\/\" target=\"_blank\">create a random number in QTP<\/a> of length 6<\/strong>. <strong>Let&#8217;s see how Len function can be used here.<\/strong> The below sample script shows some generic examples as well as the real life example (discussed above) on how you can use Len function.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'========== Generic Example ==========\r\n'Use string in Len function\r\nmsgbox Len(&quot;automation repository&quot;) 'msgbox displays value 21\r\n\r\n'Using a variable as a parameter in Len function\r\nsVar = &quot;automation repository&quot;\r\nmsgbox Len(sVar) 'msgbox displays value 21\r\n'\r\n'\r\n'========== Real Life Scenario ==========\r\n'Find Random Number of length 6\r\nRandomize\r\niTmp = Int((999999 * Rnd) + 1)\r\n\r\n'Find the Length of iTmp\r\niLen = Len(iTmp)\r\n\r\n'Add trailing zeros at the end of random number in case the Length is less that 6\r\niRandomNumber = iTmp * (10^(6 - iLen))\r\n<\/pre>\n<p>In the above example, you would see that the function Rnd returns a value that is between 0 and 1. That is, the value returned can be 0.5, 0.038, 0.00046 and so on. So when we get the random number, its length can be 6 or less than 6. If the length is less that 6 (which we find out using Len function), we can then append zero&#8217;s at the end of the random number to get a 6 digit number.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>LCase Function<\/h3>\n<p><strong>Purpose: <\/strong>Using LCase function, you can convert all the characters in a string into lowercase. For example, LCase function will convert &#8220;AuTOmATioN-1234&#8221; to &#8220;automation-1234&#8221;. You can see here that it is not necessary that the string you want to use should have characters only. This function just converts the upper case letters to lower case and ignores the rest of the characters, numbers etc.<\/p>\n<p><strong>Syntax: <\/strong>LCase(string)<\/p>\n<p><strong>Example: <\/strong>Consider a scenario where you have to perform some action based on some user input. If the user input is &#8220;Yes&#8221;, then you need to perform the action, else you don&#8217;t. Now, it is not necessary that the user will always provide the value &#8220;Yes&#8221;. User input can be &#8220;YES&#8221;, &#8220;yes&#8221;, &#8220;yEs&#8221; and so on. <strong>Also please keep in mind that in VBScript, &#8220;Yes&#8221; is not equal to &#8220;YES&#8221;.<\/strong> Let&#8217;s see how this can be done in normal way and how LCase function can simplify your job in this scenario.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'========== Normal Method ==========\r\nsUserInput = {Get user input}\r\n\r\n'Check if user input is yes or not\r\nIf sUserInput = &quot;Yes&quot; or sUserInput = &quot;yes&quot; or sUserInput = &quot;YES&quot; or sUserInput = &quot;YEs&quot; Then ' and so on........\r\n   'Do the required action\r\nEnd If\r\n\r\n'\r\n'\r\n'========== Use LCase Method ==========\r\nsUserInput = {Get user input}\r\n\r\nIf LCase(sUserInput) = &quot;yes&quot; Then 'LCase will convert all the possible combinations to lower case\r\n   'Do the required action\r\nEnd If\r\n<\/pre>\n<p><span><br \/>\n<\/span><\/p>\n<h3>UCase Function<\/h3>\n<p><strong>Purpose: <\/strong>You can use VBScript UCase function to convert all the characters in a string to uppercase. This function works the same way as LCase function, the only difference being that it will convert the characters into uppercase. For example &#8211; this function will convert &#8220;auTOmaTion+123&#8221; to &#8220;AUTOMATION+123&#8221;.<\/p>\n<p><strong>Syntax: <\/strong>UCase(string)<\/p>\n<p><strong>Example: <\/strong> This function can also be used for the same scenario that we discussed for LCase function.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nsUserInput = {Get user input}\r\n\r\nIf UCase(sUserInput) = &quot;YES&quot; Then\r\n   'Do the required action\r\nEnd If\r\n<\/pre>\n<p><span><br \/>\n<\/span><\/p>\n<h3>Trim, LTrim and RTrim Functions<\/h3>\n<p><strong>Purpose: <\/strong>In QTP, <span style=\"text-decoration: underline;\">Trim<\/span> function can be used to remove the blank spaces from the beginning and end of a string. For example, Trim(&#8221; automation repository &#8220;) would return &#8220;automation repository&#8221;. So here, the function Trim removed all the blank spaces from the beginning and end of the string.<\/p>\n<p><span style=\"text-decoration: underline;\">LTrim<\/span> (can also be called Left Trim) function removes the blank spaces from the left, ie the beginning of the string. So LTrim(&#8221; automation repository &#8220;) would return &#8220;automation repository &#8221;<\/p>\n<p>You can use <span style=\"text-decoration: underline;\">RTrim<\/span> (also called Right Trim) function in QTP remove the blank spaces from the right side or the end of the string. RTrim(&#8221; automation repository &#8220;) would return &#8221; automation repository&#8221;<\/p>\n<p><strong>Syntax: <\/strong>Trim(string), LTrim(string), RTrim(string)<\/p>\n<p><strong>Example: <\/strong>Many a times, when you extract some value from a WebElement or a table cell, you would notice that the string that you have extracted may contain leading or trailing blank spaces. Now, if you don&#8217;t remove these spaces from the string and compare this string with some value, the script would not match the strings because of the blank spaces. So in such situations you would need to use VBScript Trim function.<\/p>\n<p>The below example shows the sample code where you need to compare the value from a WebElement with some value from the data sheet.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDim iDataFromApplication, iDataFromDataSheet\r\n\r\n'Retrieve both the values\r\niDataFromDataSheet = fnGetDataFromDataSheet() 'Some function to get data from data sheet\r\niDataFromApplication = Browser(&quot;Brw&quot;).Page(&quot;Pg&quot;).WebElement(&quot;WebEl&quot;).GetROProperty(&quot;innertext&quot;)\r\n\r\n'Compare the values\r\nIf Trim(iDataFromApplication) = iDataFromDataSheet Then\r\n   Reporter.ReportEvent micPass, &quot;Passed&quot;, &quot;Values Match&quot;\r\nElse\r\n   Reporter.ReportEvent micFail, &quot;Failed&quot;, &quot;Values DONT Match&quot;\r\nEnd If\r\n<\/pre>\n<p>Now most of the times, we use the Trim function just as a precautionary step to make sure that the string doesn&#8217;t contain any leading or trailing spaces. So it makes sense to check for the presence of spaces both at the beginning and end of string. Because of this reason, you would notice that you would be using Trim function at many places but LTrim and RTrim would be used only in rare scenarios.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>InStr Function<\/h3>\n<p><strong>Purpose: <\/strong>InStr is one of the most common string manipulation functions that you would be using in QTP. InStr function helps you determine if a smaller string is present in a bigger string or not. For example, you can use this function to determine if the string &#8220;Test&#8221; (smaller string) is present in &#8220;QuickTest&#8221; (bigger string).<\/p>\n<p>If a match is found, this functions returns a number that represents the position at which the match is found. For example, if you use InStr to search if &#8220;tom&#8221; is present in &#8220;automation&#8221;, the function InStr would return the value 3 (which is the starting position of &#8220;tom&#8221;). So all we need to do here is to check the return value. If it is a number greater than 0 that means the match is found.<\/p>\n<p><strong>Syntax: <\/strong>InStr(Start(optional), BigString, SmallString, Compare(optional))<\/p>\n<ul>\n<li><span style=\"text-decoration: underline;\">Start<\/span> is an optional argument which specifies the position from where the search should start in the BigString. If this value is not provided, the search would start from first character.<\/li>\n<li><span style=\"text-decoration: underline;\">BigString<\/span> is the string in which you will search for the smaller string.<\/li>\n<li><span style=\"text-decoration: underline;\">SmallString<\/span> is the string which will be searched in the bigger or main string.<\/li>\n<li><span style=\"text-decoration: underline;\">Compare<\/span> is an optional argument which specifies the type of search. Value 0 means binary comparison and value 1 means text comparison.<\/li>\n<\/ul>\n<p><strong>Example: <\/strong>Let&#8217;s understand this with the help of few examples.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nsBigString = &quot;Automation Repository&quot;\r\nsSmallString = &quot;on Rep&quot;\r\n\r\n'Start match from 1st character\r\niReturnVal = InStr(1, sBigString, sSmallString, 1) 'iReturnVal will contain the value 9\r\n\r\n'Start match from 11th character\r\niReturnVal = InStr(11, sBigString, sSmallString, 1)\r\n'From the above statement iReturnVal will have value 0 because we are matching from 11th character.\r\n'So effectively, we are trying to find &quot;on Rep&quot; in &quot;Repository&quot;\r\n\r\n'Start match from 1st character without specifying the 1st argument\r\nsSmallString2 = &quot;Auto&quot;\r\niReturnVal = InStr(sBigString, sSmallString2) 'iReturnVal will contain the value 1\r\n<\/pre>\n<p>Please note that, if you have omitted the 1st argument, you would need to omit the last argument also. Omitting the first argument and keeping the last one will give you an error.<\/p>\n<p><strong>Real Life Example: <\/strong>The above example was just to show the basic working of this function. But in real life scenarios, you would mostly be verifying if a string is available inside another string or not. Let&#8217;s see an example for this.<\/p>\n<p>Imagine you are automating a web application where you have the functionality to send emails also. You have to send an email and verify if success message was displayed or not. The below image shows the success and failure message.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"QTP InStr Function Example\" src=\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-instr-example.png\" alt=\"QTP InStr Function Example\" width=\"585\" height=\"115\" \/><\/p>\n<p>From the above figure, you can note that when the email is sent successfully, the applications shows the message &#8211; &#8220;Email Successfully Sent to \u2013 {some email id}&#8221;. Here, the mail ids can change but the message before that remains same. So in your QTP script, you can use the InStr function to verify if the message is displayed or not.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nsMessageFromApplication = Browser(&quot;Brw&quot;).Page(&quot;Pg&quot;).WebElement(&quot;WebEl&quot;).GetROProperty(&quot;innertext&quot;)\r\n\r\n'Check if the mail is successfully displayed or not\r\nIf InStr(sMessageFromApplication, &quot;Email Successfully Sent to&quot;) &lt;&gt; 0 Then\r\n   Reporter.ReportEvent micPass, &quot;Pass&quot;, &quot;Email Sent Successfully&quot;\r\nElse\r\n   Reporter.ReportEvent micFail, &quot;Fail&quot;, &quot;Email NOT sent&quot;\r\nEnd If\r\n<\/pre>\n<p><span><br \/>\n<\/span><\/p>\n<h3>Split Function<\/h3>\n<p><strong>Purpose: <\/strong>This is another very common string manipulation function that you would find yourself using very often in your QTP Scripts. This function splits a string using a delimiter and returns a number of sub-strings stored in an array. For example, if you want to split the string &#8220;QuickTest Professional&#8221; using &#8221; &#8221; (blank space) as a delimiter, the split function would return 2 sub-strings &#8211; &#8220;QuickTest&#8221; and &#8220;Professional&#8221;. Both these sub-strings will be stored in an array. The first sub-string in index 0 of the array and the second sub-string in index 1 of the array.<\/p>\n<p><strong>Syntax: <\/strong>Split(String, Delimiter(optional), Count(optional), Compare(optional))<\/p>\n<ul>\n<li><span style=\"text-decoration: underline;\">String<\/span> is the text that you want to split.<\/li>\n<li><span style=\"text-decoration: underline;\">Delimiter<\/span> is the character or string using which you want to split the main string. If omitted, the space character (&#8221; &#8220;) is taken as the delimiter.<\/li>\n<li><span style=\"text-decoration: underline;\">Count<\/span> is the number of sub-strings that you want the function to return. The value -1 means that all the sub-strings will be returned.<\/li>\n<li><span style=\"text-decoration: underline;\">Compare<\/span> indicates the kind of sub-strings used while evaluating sub-strings. Value 0 indicates binary comparison and value 1 indicates textual comparison.<\/li>\n<\/ul>\n<p><strong>Example: <\/strong>Let&#8217;s see some examples to understand this function clearly.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nsText = &quot;Yes,No,Maybe&quot;\r\nsDelimiter = &quot;,&quot; 'Delimiter is comma (,)\r\n\r\n'Split the text using the delimiter. 'arrArray' is the array in which the fucntion Split will store the sub-strings\r\narrArray = Split(sText, sDelimiter)\r\n'arrArray(0) contains the value Yes. arrArray(1) contains the value No and  arrArray(2 contains the value Maybe\r\n\r\n'Display the sub-strings in a msgbox\r\niLoop = UBound(arrArray) 'Find trhe size of the array\r\nFor i = 0 to iLoop\r\n   msgbox arrArray(i)\r\nNext\r\n\r\n'\r\n'Split the text using the delimiter but specify the count of the sub-strings\r\narrArray1 = Split(sText, sDelimiter, 2)\r\n'Since we have specified the count of sub-strings as 2, arrArray1(0) will have Yes and arrArray1(1) will have the value No,Maybe\r\n\r\n'Display the sub-strings in a msgbox\r\niLoop1 = UBound(arrArray1) 'Find trhe size of the array\r\nFor i = 0 to iLoop1\r\n   msgbox arrArray1(i)\r\nNext\r\n<\/pre>\n<p><strong>Real Life Example of Split function in QTP. <\/strong>There is a very common scenario where you would be using the Split function in your QTP scripts. Consider a situation where you have a drop down field in an application and you want to find out if a particular value is available in the drop down or not. This task can be performed using the split function. If you spy on a drop down field, you would notice that it has a property called &#8220;all items&#8221;. This property contains all the items in the drop-down separated mostly by a semi-colon(;).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"QTP Split Function Example\" src=\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-split-function-example.png\" alt=\"QTP Split Function Example\" width=\"139\" height=\"90\" \/><\/p>\n<p>The above figure shows a drop down field with all its items. The &#8220;all items&#8221; property for this drop down field will have this value &#8211; &#8220;Red;Green;Blue;Orange;Yellow&#8221;. Now your task is to find out if the value &#8216;Orange&#8217; is available in that list or not. Let&#8217;s see how this can be accomplished.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Get the list of all the items in the drop down field\r\nsAll_Items = Browser(&quot;Brw&quot;).Page(&quot;Pg&quot;).WebList(&quot;WebL&quot;).GetROProperty(&quot;all items&quot;)\r\n\r\n'Split the string using ; as the delimiter\r\narrItems = Split(sAll_Items, &quot;;&quot;)\r\niItemCount = UBound(arrItems)\r\n\r\n'Check if the item 'Orange' is available in the list or not\r\nflValueFound = False\r\nFor i=0 to iItemCount\r\n   If arrItems(i) = &quot;Orange&quot; Then\r\n      flValueFound = True\r\n\t  Exit For\r\n   End If\r\nNext\r\n\r\n'Report the Result\r\nIf flValueFound = True Then\r\n   Reporter.ReportEvent micPass, &quot;Pass&quot;, &quot;Value found in the WebList&quot;\r\nElse\r\n   Reporter.ReportEvent micFail, &quot;Failed&quot;, &quot;Value NOT found in the WebList&quot;\r\nEnd If\r\n<\/pre>\n<p><span><br \/>\n<\/span><\/p>\n<h3>Left Function<\/h3>\n<p><strong>Purpose: <\/strong>In QTP, you can use the Left function to find out a specified number of characters from the left of a string. For example, if you want to find the first 2 characters in the string &#8220;QuickTest Pro&#8221;, the Left function would return the value &#8220;Qu&#8221;.<\/p>\n<p><strong>Syntax: <\/strong>Left(String, Length) where String is the actual string and Length is the number of characters you want to find.<\/p>\n<p><strong>Example: <\/strong>Let&#8217;s see the example of Left function.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDim sMainString, sLeftString\r\nsMainString = &quot;Orange&quot;\r\nsLeftString = Left(sMainString, 2) 'Get the first two characters from the left of the string\r\nmsgbox sLeftString 'Displays the value &quot;Or&quot;\r\n<\/pre>\n<p><span><br \/>\n<\/span><\/p>\n<h3>Right Function<\/h3>\n<p><strong>Purpose: <\/strong>The Right function works the same way as Left function with the only difference being that the Right function retrieves the specified characters from the right side of the string. So, if you want to find the 3 right characters in the string &#8220;VBScript&#8221;, the function Right will return the value &#8220;ipt&#8221;.<\/p>\n<p><strong>Syntax: <\/strong>Right(String, Length)<\/p>\n<p><strong>Example: <\/strong>A very good place where you can use the Right function is the GMail Inbox. Let us suppose that your task is to find out the number of emails that are delivered to you in GMail today. If you observe the GMail inbox, you would notice that all the mails delivered today will have the time in &#8220;am&#8221; or &#8220;pm&#8221; at the end. All other mails will have a date at the end. Refer the below image for screenshot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" title=\"QTP Right Function Example\" src=\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-right-function-example.png\" alt=\"QTP Right Function Example\" width=\"294\" height=\"260\" \/><\/p>\n<p>From this, you can easily make out that you have to find the text from each row and then use the Right function to find out the last 2 characters. If the last 2 characters is &#8220;am&#8221; or &#8220;pm&#8221; then that mail is delivered today. Let&#8217;s see the code for this.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Search for emails received today in your inbox\r\n'Logic - The mails received today will have only time in the last(8th) column.\r\n'Time is always followed by am or pm. So the code check for the last 2 characters and matches it with am or pm.\r\nFor iR = 1 to 50\r\n    sLastColumnText = Browser(&quot;Inbox&quot;).Page(&quot;Inbox&quot;).Frame(&quot;Frame&quot;).WebTable(&quot;Emails&quot;).GetCellData(iR,8)\r\n    sLast2Characters = Right(sLastColumnText, 2)\r\n    If sLast2Characters = &quot;am&quot; or sLast2Characters = &quot;pm&quot; Then\r\n        iTodayMails = iTodayMails + 1\r\n    Else\r\n        'Exit For\r\n    End If\r\nNext\r\n'Report the number of  mails received today\r\nReporter.ReportEvent micPass, &quot;Total Emails Received Today - &quot; &amp; iTodayMails, &quot;&quot;\r\n<\/pre>\n<p><span><br \/>\n<\/span><\/p>\n<h3>Mid Function<\/h3>\n<p><strong>Purpose: <\/strong>In QTP, Mid function can be used to retrieve a sub-string from a main string. For example, if you have a string like &#8220;VBScript&#8221;, you can use the mid function to extract sub-strings like &#8220;VBS&#8221;, &#8220;Script&#8221; etc.<\/p>\n<p><strong>Syntax: <\/strong>Mid(String, Start, Length(optional))<\/p>\n<ul>\n<li><span style=\"text-decoration: underline;\">String<\/span> is the actual string from which you want to extract a sub-string.<\/li>\n<li><span style=\"text-decoration: underline;\">Start<\/span> is the starting position from where the sub-string should be extracted.<\/li>\n<li><span style=\"text-decoration: underline;\">Length<\/span> is the length of the sub-string to be extracted. If omitted, all the characters from the <span style=\"text-decoration: underline;\">Start<\/span> value till the end of the main string are taken as part of the sub-string.<\/li>\n<\/ul>\n<p><strong>Example: <\/strong><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDim sMainString, sSubString, sSubString1\r\nsMainString = &quot;Orange&quot;\r\n\r\nsSubString = Mid(sMainString, 3, 2) ' variable 'sSubString' will have the value 'an'\r\n\r\nsSubString1 = Mid(sMainString, 3) ' variable 'sSubString1' will have the value 'ange'\r\n<\/pre>\n<p><strong>Real Life Example of Mid Function in QTP. <\/strong>If you recall the example that we had used with InStr function above, we used the function to find if the email success message was delivered or not. Now let us assume that you want to find out the email id to which the mail was sent from the success message (&#8220;Email Successfully Sent to \u2013 anish@automationrepository.com&#8221;). Here you can see that the text before the email id is always fixed. So you can count the starting point. Also the length of the email id is not fixed. But it comes at the end of the string. So you can omit the Length parameter so that the sub-string that is returned contains the full email id.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>Few more functions<\/h3>\n<p>Till now, we have covered only those VBScript string manipulation functions that you would be using very commonly in QTP. However, there are few more function which you would be using very rarely (or maybe not at all). Below is the list of these functions and a one liner explanation of each.<\/p>\n<p><strong>InStrRev Function <\/strong>This function is similar to InStr function with the only difference being that it returns the position of the occurrence of a sub-string from the end of the string.<\/p>\n<p><strong>Replace Function <\/strong>This function can be used to replace some sub-string within a main string with some other string. For example, if you have a string like &#8220;abaacaadea&#8221;, you can use the replace function to replace the character &#8216;a&#8217; (or any sub-string) with any other character or sub-string.<\/p>\n<p><strong>Space Function <\/strong>You can use the Space function in QTP to create a string with a given number of blank spaces. For example, var = Space(5) would return string &#8211; &#8221; &#8220;, i.e. a string with 5 blank spaces.<\/p>\n<p><strong>String Function <\/strong>You can use the String function to create a repeating character string of a specified length. str = String(5, &#8220;a&#8221;) would return the string &#8211; &#8220;aaaaa&#8221;.<\/p>\n<p><strong>StrReverse Function <\/strong>StrReverse function in QTP can be used to create a reverse of a string. For example, str = StrReverse(&#8220;orange&#8221;) would return the string &#8211; &#8220;egnaro&#8221;.<\/p>\n<p><strong>Join Function <\/strong>If you have a number of sub-strings in an array, you can use the Join function to concatenate all the sub-strings from the array into a single string.<\/p>\n<p><strong>StrComp Function <\/strong>StrComp function can be used to compare two strings in QTP. You can choose to ignore this function because you can perform the same task of comparing the strings using comparison operators.<\/p>\n<p><strong>Filter Function <\/strong>This function returns a zero-based array containing a subset of a string array based on a specified filter criteria.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>Practice Questions<\/h3>\n<p>If you have thoroughly understood the string manipulation functions covered above, you can try to write programs for the below mentioned practice questions. This would help you get a good grasp of the concepts discussed above.<\/p>\n<ul>\n<li><strong>1)<\/strong> Given a string, write a program that finds out all the characters that constitute the string. Example &#8211; If the string is VBScript, the output (using a msgbox) should be in this format &#8211; The string VBScript contains variables &#8211; V, B, S, c, r, i, p, t<\/li>\n<li><strong>2)<\/strong> Write a program that mimics the functionality of LCase function. For example, your program should take any variable like &#8220;AuTOMatIon123_!@#&#8221; and convert all the upper case letter to lower case like &#8220;automation123_!@#&#8221;.<\/li>\n<li><strong>3)<\/strong> Write a program that reverses the order of words (not characters) in a sentence. For example, the program should change the sentence &#8220;Good Morning Everybody&#8221; to &#8220;Everybody Morning Good&#8221;<\/li>\n<\/ul>\n<p><em> <\/em><br \/>\nYou can post your answers to these practice questions in the comments section.<\/p>\n<p><strong>Let us know what you think of this article. If you have any suggestions or feedback, please feel free to drop in a note in the comments section or email me at anish [at] automationrepository [dot] com. Thanks for visiting&#8230;<\/strong><br \/>\n<span><br \/>\n<\/span><\/p>\n<\/div>\n<div style=\"font-family: Verdana,sans-serif; border: 1px solid #C8B560; padding: 3mm; background: #FFF8C6; text-align: center;\">\n<p><strong>If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.<\/strong><\/p>\n<form style=\"text-align: center;\" action=\"http:\/\/feedburner.google.com\/fb\/a\/mailverify\" method=\"post\" onsubmit=\"window.open('http:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=automationrepository\/feeds', 'popupwindow', 'scrollbars=yes,width=550,height=520');return true\" target=\"popupwindow\"><input style=\"width: 160px; height: 20px;\" onfocus=\"if (this.value == 'Enter Your Email Address') {this.value = '';}\" onblur=\"if (this.value == '') {this.value = 'Enter Your Email Address';}\" type=\"text\" value=\"Enter Your Email Address\" \/> <input type=\"hidden\" name=\"uri\" value=\"automationrepository\/feeds\" \/> <input type=\"hidden\" name=\"loc\" value=\"en_US\" \/> <input type=\"submit\" value=\"Join Us\" \/><\/p>\n<p style=\"text-align: justify;\">\n<\/form>\n<\/div>\n<p style=\"border: 1px solid #C38EC7; padding: 3mm; background: #EBDDE2;\">To check out more tutorials, visit our <a title=\"QTP Tutorials\" href=\"http:\/\/www.automationrepository.com\/tutorials-for-qtp-beginners\/\" target=\"_blank\">QTP Tutorials<\/a> page. You can also check the <a title=\"All Articles\" href=\"http:\/\/www.automationrepository.com\/archive\/\" target=\"_blank\">Archives<\/a> page to view the list of all our articles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article (and few more upcoming articles on VBScript Function Reference) is an extension of the QTP VBScript Tutorial series that we had started sometime back. In this article, you would see some of the important and common VBScript string manipulation functions that you would most likely be using almost daily while working with QTP.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64,4,37],"tags":[112,107,111,114,109,108,115,106,110,113],"class_list":["post-1569","post","type-post","status-publish","format-standard","hentry","category-qtp-and-vbscript-tutorials","category-qtp-basic-stuff","category-qtp-tutorials-for-beginners","tag-lcase-function","tag-left-function","tag-len-function","tag-ltrim-function","tag-mid-function","tag-right-function","tag-rtrim-function","tag-split-function","tag-trim-function","tag-ucase-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Common string manipulation fuinctions that can be used in QTP<\/title>\n<meta name=\"description\" content=\"This article contains the list of the most common string manipulation functions that you can use in QTP. The article explains each of these functions in detail.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anish Pillai\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/\",\"name\":\"Common string manipulation fuinctions that can be used in QTP\",\"isPartOf\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-vbscript-function-library.png\",\"datePublished\":\"2012-05-05T22:38:38+00:00\",\"dateModified\":\"2012-05-05T22:39:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\"},\"description\":\"This article contains the list of the most common string manipulation functions that you can use in QTP. The article explains each of these functions in detail.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#primaryimage\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-vbscript-function-library.png\",\"contentUrl\":\"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-vbscript-function-library.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.automationrepository.com\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Common string manipulation functions that you would be using almost daily in QTP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/\",\"name\":\"XX\",\"description\":\"\\r\\nasas\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\",\"name\":\"Anish Pillai\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"caption\":\"Anish Pillai\"},\"description\":\"Find more about Anish Pillai on Google+\",\"sameAs\":[\"http:\/\/www.automationrepository.com\"],\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Common string manipulation fuinctions that can be used in QTP","description":"This article contains the list of the most common string manipulation functions that you can use in QTP. The article explains each of these functions in detail.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/","twitter_misc":{"Written by":"Anish Pillai","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/","url":"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/","name":"Common string manipulation fuinctions that can be used in QTP","isPartOf":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#primaryimage"},"image":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#primaryimage"},"thumbnailUrl":"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-vbscript-function-library.png","datePublished":"2012-05-05T22:38:38+00:00","dateModified":"2012-05-05T22:39:04+00:00","author":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20"},"description":"This article contains the list of the most common string manipulation functions that you can use in QTP. The article explains each of these functions in detail.","breadcrumb":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#primaryimage","url":"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-vbscript-function-library.png","contentUrl":"https:\/\/www.automationrepository.com\/wordpress\/wp-content\/uploads\/ar\/qtp-vbscript-function-library.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/05\/vbscript-string-manipulation-functions-with-examples-in-qtp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.automationrepository.com\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Common string manipulation functions that you would be using almost daily in QTP"}]},{"@type":"WebSite","@id":"https:\/\/www.automationrepository.com\/wordpress\/#website","url":"https:\/\/www.automationrepository.com\/wordpress\/","name":"XX","description":"\r\nasas","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20","name":"Anish Pillai","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","caption":"Anish Pillai"},"description":"Find more about Anish Pillai on Google+","sameAs":["http:\/\/www.automationrepository.com"],"url":"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/"}]}},"_links":{"self":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1569"}],"version-history":[{"count":22,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1569\/revisions"}],"predecessor-version":[{"id":1601,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1569\/revisions\/1601"}],"wp:attachment":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}