site
stats
By Anish Pillai Anish Pillai Posted under qtp and vbscript tutorials | QTP Basic Stuff | QTP Tutorials for Beginners

Common string manipulation functions that you would be using almost daily in QTP

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

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. We will explain each of these functions in depth using examples and code snippets.

NOTE: 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.


Why this article?

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 “learn while you do” 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.

If you are beginning to learn QTP, I’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. 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. This would help you understand these concepts and their application in a better manner.

Classification of VBScript Function Library

For the ease of understanding, we will divide the entire VBScript function library that we use in QTP into following four types –

QTP VBScript Function Library - String Manipulation Functions

Let’s now start with VBScript string manipulation functions that we use in QTP.

Len Function

Purpose: 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.

Syntax: Len(string)
With this function, you can directly pass a string as an argument or you can pass a variable also.

Example: Let us suppose that you want to create a random number in QTP of length 6. Let’s see how Len function can be used here. The below sample script shows some generic examples as well as the real life example (discussed above) on how you can use Len function.

'========== Generic Example ==========
'Use string in Len function
msgbox Len("automation repository") 'msgbox displays value 21

'Using a variable as a parameter in Len function
sVar = "automation repository"
msgbox Len(sVar) 'msgbox displays value 21
'
'
'========== Real Life Scenario ==========
'Find Random Number of length 6
Randomize
iTmp = Int((999999 * Rnd) + 1)

'Find the Length of iTmp
iLen = Len(iTmp)

'Add trailing zeros at the end of random number in case the Length is less that 6
iRandomNumber = iTmp * (10^(6 - iLen))

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’s at the end of the random number to get a 6 digit number.

LCase Function

Purpose: Using LCase function, you can convert all the characters in a string into lowercase. For example, LCase function will convert “AuTOmATioN-1234” to “automation-1234”. 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.

Syntax: LCase(string)

Example: Consider a scenario where you have to perform some action based on some user input. If the user input is “Yes”, then you need to perform the action, else you don’t. Now, it is not necessary that the user will always provide the value “Yes”. User input can be “YES”, “yes”, “yEs” and so on. Also please keep in mind that in VBScript, “Yes” is not equal to “YES”. Let’s see how this can be done in normal way and how LCase function can simplify your job in this scenario.

'========== Normal Method ==========
sUserInput = {Get user input}

'Check if user input is yes or not
If sUserInput = "Yes" or sUserInput = "yes" or sUserInput = "YES" or sUserInput = "YEs" Then ' and so on........
   'Do the required action
End If

'
'
'========== Use LCase Method ==========
sUserInput = {Get user input}

If LCase(sUserInput) = "yes" Then 'LCase will convert all the possible combinations to lower case
   'Do the required action
End If


UCase Function

Purpose: 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 – this function will convert “auTOmaTion+123” to “AUTOMATION+123”.

Syntax: UCase(string)

Example: This function can also be used for the same scenario that we discussed for LCase function.

sUserInput = {Get user input}

If UCase(sUserInput) = "YES" Then
   'Do the required action
End If


Trim, LTrim and RTrim Functions

Purpose: In QTP, Trim function can be used to remove the blank spaces from the beginning and end of a string. For example, Trim(” automation repository “) would return “automation repository”. So here, the function Trim removed all the blank spaces from the beginning and end of the string.

LTrim (can also be called Left Trim) function removes the blank spaces from the left, ie the beginning of the string. So LTrim(” automation repository “) would return “automation repository ”

You can use RTrim (also called Right Trim) function in QTP remove the blank spaces from the right side or the end of the string. RTrim(” automation repository “) would return ” automation repository”

Syntax: Trim(string), LTrim(string), RTrim(string)

Example: 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’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.

The below example shows the sample code where you need to compare the value from a WebElement with some value from the data sheet.

Dim iDataFromApplication, iDataFromDataSheet

'Retrieve both the values
iDataFromDataSheet = fnGetDataFromDataSheet() 'Some function to get data from data sheet
iDataFromApplication = Browser("Brw").Page("Pg").WebElement("WebEl").GetROProperty("innertext")

'Compare the values
If Trim(iDataFromApplication) = iDataFromDataSheet Then
   Reporter.ReportEvent micPass, "Passed", "Values Match"
Else
   Reporter.ReportEvent micFail, "Failed", "Values DONT Match"
End If

Now most of the times, we use the Trim function just as a precautionary step to make sure that the string doesn’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.

InStr Function

Purpose: 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 “Test” (smaller string) is present in “QuickTest” (bigger string).

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 “tom” is present in “automation”, the function InStr would return the value 3 (which is the starting position of “tom”). 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.

Syntax: InStr(Start(optional), BigString, SmallString, Compare(optional))

  • Start 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.
  • BigString is the string in which you will search for the smaller string.
  • SmallString is the string which will be searched in the bigger or main string.
  • Compare is an optional argument which specifies the type of search. Value 0 means binary comparison and value 1 means text comparison.

Example: Let’s understand this with the help of few examples.

sBigString = "Automation Repository"
sSmallString = "on Rep"

'Start match from 1st character
iReturnVal = InStr(1, sBigString, sSmallString, 1) 'iReturnVal will contain the value 9

'Start match from 11th character
iReturnVal = InStr(11, sBigString, sSmallString, 1)
'From the above statement iReturnVal will have value 0 because we are matching from 11th character.
'So effectively, we are trying to find "on Rep" in "Repository"

'Start match from 1st character without specifying the 1st argument
sSmallString2 = "Auto"
iReturnVal = InStr(sBigString, sSmallString2) 'iReturnVal will contain the value 1

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.

Real Life Example: 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’s see an example for this.

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.

QTP InStr Function Example

From the above figure, you can note that when the email is sent successfully, the applications shows the message – “Email Successfully Sent to – {some email id}”. 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.

sMessageFromApplication = Browser("Brw").Page("Pg").WebElement("WebEl").GetROProperty("innertext")

'Check if the mail is successfully displayed or not
If InStr(sMessageFromApplication, "Email Successfully Sent to") <> 0 Then
   Reporter.ReportEvent micPass, "Pass", "Email Sent Successfully"
Else
   Reporter.ReportEvent micFail, "Fail", "Email NOT sent"
End If


Split Function

Purpose: 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 “QuickTest Professional” using ” ” (blank space) as a delimiter, the split function would return 2 sub-strings – “QuickTest” and “Professional”. 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.

Syntax: Split(String, Delimiter(optional), Count(optional), Compare(optional))

  • String is the text that you want to split.
  • Delimiter is the character or string using which you want to split the main string. If omitted, the space character (” “) is taken as the delimiter.
  • Count 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.
  • Compare indicates the kind of sub-strings used while evaluating sub-strings. Value 0 indicates binary comparison and value 1 indicates textual comparison.

Example: Let’s see some examples to understand this function clearly.

sText = "Yes,No,Maybe"
sDelimiter = "," 'Delimiter is comma (,)

'Split the text using the delimiter. 'arrArray' is the array in which the fucntion Split will store the sub-strings
arrArray = Split(sText, sDelimiter)
'arrArray(0) contains the value Yes. arrArray(1) contains the value No and  arrArray(2 contains the value Maybe

'Display the sub-strings in a msgbox
iLoop = UBound(arrArray) 'Find trhe size of the array
For i = 0 to iLoop
   msgbox arrArray(i)
Next

'
'Split the text using the delimiter but specify the count of the sub-strings
arrArray1 = Split(sText, sDelimiter, 2)
'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

'Display the sub-strings in a msgbox
iLoop1 = UBound(arrArray1) 'Find trhe size of the array
For i = 0 to iLoop1
   msgbox arrArray1(i)
Next

Real Life Example of Split function in QTP. 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 “all items”. This property contains all the items in the drop-down separated mostly by a semi-colon(;).

QTP Split Function Example

The above figure shows a drop down field with all its items. The “all items” property for this drop down field will have this value – “Red;Green;Blue;Orange;Yellow”. Now your task is to find out if the value ‘Orange’ is available in that list or not. Let’s see how this can be accomplished.

'Get the list of all the items in the drop down field
sAll_Items = Browser("Brw").Page("Pg").WebList("WebL").GetROProperty("all items")

'Split the string using ; as the delimiter
arrItems = Split(sAll_Items, ";")
iItemCount = UBound(arrItems)

'Check if the item 'Orange' is available in the list or not
flValueFound = False
For i=0 to iItemCount
   If arrItems(i) = "Orange" Then
      flValueFound = True
	  Exit For
   End If
Next

'Report the Result
If flValueFound = True Then
   Reporter.ReportEvent micPass, "Pass", "Value found in the WebList"
Else
   Reporter.ReportEvent micFail, "Failed", "Value NOT found in the WebList"
End If


Left Function

Purpose: 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 “QuickTest Pro”, the Left function would return the value “Qu”.

Syntax: Left(String, Length) where String is the actual string and Length is the number of characters you want to find.

Example: Let’s see the example of Left function.

Dim sMainString, sLeftString
sMainString = "Orange"
sLeftString = Left(sMainString, 2) 'Get the first two characters from the left of the string
msgbox sLeftString 'Displays the value "Or"


Right Function

Purpose: 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 “VBScript”, the function Right will return the value “ipt”.

Syntax: Right(String, Length)

Example: 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 “am” or “pm” at the end. All other mails will have a date at the end. Refer the below image for screenshot.

QTP Right Function Example

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 “am” or “pm” then that mail is delivered today. Let’s see the code for this.

'Search for emails received today in your inbox
'Logic - The mails received today will have only time in the last(8th) column.
'Time is always followed by am or pm. So the code check for the last 2 characters and matches it with am or pm.
For iR = 1 to 50
    sLastColumnText = Browser("Inbox").Page("Inbox").Frame("Frame").WebTable("Emails").GetCellData(iR,8)
    sLast2Characters = Right(sLastColumnText, 2)
    If sLast2Characters = "am" or sLast2Characters = "pm" Then
        iTodayMails = iTodayMails + 1
    Else
        'Exit For
    End If
Next
'Report the number of  mails received today
Reporter.ReportEvent micPass, "Total Emails Received Today - " & iTodayMails, ""


Mid Function

Purpose: In QTP, Mid function can be used to retrieve a sub-string from a main string. For example, if you have a string like “VBScript”, you can use the mid function to extract sub-strings like “VBS”, “Script” etc.

Syntax: Mid(String, Start, Length(optional))

  • String is the actual string from which you want to extract a sub-string.
  • Start is the starting position from where the sub-string should be extracted.
  • Length is the length of the sub-string to be extracted. If omitted, all the characters from the Start value till the end of the main string are taken as part of the sub-string.

Example:

Dim sMainString, sSubString, sSubString1
sMainString = "Orange"

sSubString = Mid(sMainString, 3, 2) ' variable 'sSubString' will have the value 'an'

sSubString1 = Mid(sMainString, 3) ' variable 'sSubString1' will have the value 'ange'

Real Life Example of Mid Function in QTP. 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 (“Email Successfully Sent to – anish@automationrepository.com”). 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.

Few more functions

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.

InStrRev Function 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.

Replace Function 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 “abaacaadea”, you can use the replace function to replace the character ‘a’ (or any sub-string) with any other character or sub-string.

Space Function 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 – ” “, i.e. a string with 5 blank spaces.

String Function You can use the String function to create a repeating character string of a specified length. str = String(5, “a”) would return the string – “aaaaa”.

StrReverse Function StrReverse function in QTP can be used to create a reverse of a string. For example, str = StrReverse(“orange”) would return the string – “egnaro”.

Join Function 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.

StrComp Function 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.

Filter Function This function returns a zero-based array containing a subset of a string array based on a specified filter criteria.

Practice Questions

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.

  • 1) Given a string, write a program that finds out all the characters that constitute the string. Example – If the string is VBScript, the output (using a msgbox) should be in this format – The string VBScript contains variables – V, B, S, c, r, i, p, t
  • 2) Write a program that mimics the functionality of LCase function. For example, your program should take any variable like “AuTOMatIon123_!@#” and convert all the upper case letter to lower case like “automation123_!@#”.
  • 3) Write a program that reverses the order of words (not characters) in a sentence. For example, the program should change the sentence “Good Morning Everybody” to “Everybody Morning Good”


You can post your answers to these practice questions in the comments section.

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…

If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.

To check out more tutorials, visit our QTP Tutorials page. You can also check the Archives page to view the list of all our articles.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • Pingback: VBScript Arithmetic functions that can be used in QTP - Automation Repository - Automation Repository()

  • ian

    hi, images are not showing up 🙁 but i like your tutorials in QTP 🙂

  • roshan

    1) Given a string, write a program that finds out all the characters that constitute the string. Example – If the string is VBScript, the output (using a msgbox) should be in this format – The string VBScript contains variables – V, B, S, c, r, i, p, t.

    'Code
    Dim str
    str = InputBox("Enterr string to be reversed","Roshan's trial prog")
    iLen = Len(str)
    For i =1 to iLen
    temp=Mid(str,i,1)
    RevStr= RevStr+temp+","
    Next
    msgbox RevStr

    • Akhilesh

      Dim str
      str = InputBox("Enterr string to be reversed :: ")
      iLen = Len(str)
      For i =1 to iLen
      temp=Mid(str,i,1)
      RevStr= RevStr+temp+","
      Next
      iLen1 = Len(RevStr)
      RevStr1 = Left(RevStr,iLen1-1)

      msgbox "String has following characters :: " &RevStr1

  • Roshan

    2) Write a program that mimics the functionality of LCase function. For example, your program should take any variable like “AuTOMatIon123_!@#” and convert all the upper case letter to lower case like “automation123_!@#”.

    'Code
    Str = InputBox("String to be lowered")
    diff = Asc("a") – Asc("A")

    For i = 1 to Len(Str)
    If Asc(Mid(Str,i,1))<=Asc("Z") And Asc(Mid(Str,i,1))>=Asc("A")Then
    If Asc(Mid(Str,i,1)) = Asc(" ") Then
    StrLower= StrLower + Mid(Str,i,1)
    End If
    temp= chr(Asc(Mid(Str,i,1))+diff)
    StrLower= StrLower+temp
    Else
    StrLower= StrLower + Mid(Str,i,1)
    End If
    Next
    MsgBox StrLower
    'End Code

  • Roshan

    3) Write a program that reverses the order of words (not characters) in a sentence. For example, the program should change the sentence “Good Morning Everybody” to “Everybody Morning Good”

    Str = InputBox("String to be Reversed")
    SplitStr= Split(Str)
    For i = UBound(SplitStr) to 0 Step -1
    StrRev=StrRev+" "+SplitStr(i)
    Next
    MsgBox StrRev

  • Ajit More

    3) Write a program that reverses the order of words (not characters) in a sentence. For example, the program should change the sentence “Good Morning Everybody” to “Everybody Morning Good”
    p = “Good Morning Everybody”
    While InStrRev( p, " ")
    ans = ans & " " & mid(p,InStrRev( p, " ")+1)
    p = trim(mid(p,1, InStrRev( p, " ")))
    Wend
    msgbox trim(ans & " " &p)

    • Anish10110

      Yep.. 🙂

  • Vani

    Above blog was very usefull

    • Anish10110

      Thank you Vani.. 🙂

  • Akhilesh

    '1) Given a string, write a program that finds out all the characters that constitute the string. 'Example – If the string is VBScript, the output (using a msgbox) should be in this format – The 'string VBScript contains variables – V, B, S, c, r, i, p, t

    Dim str
    str = InputBox("Enterr string to be reversed :: ")
    iLen = Len(str)
    For i =1 to iLen
    temp=Mid(str,i,1)
    RevStr= RevStr+temp+","
    Next
    iLen1 = Len(RevStr)
    RevStr1 = Left(RevStr,iLen1-1)

    msgbox "String has following characters :: " &RevStr1

  • Poovarasan

    '1) Given a string, write a program that finds out all the characters that constitute the string. Example – If the string is VBScript, the output (using a msgbox) should be in this format – The string VBScript contains variables – V, B, S, c, r, i, p, t
    'Answer:
    sString = "VBScript" 'InputBox("Enter your input value?")
    sStrLen = Len(sString)

    For i=1 to sStrLen
    If i=sStrLen Then
    sStrResult = sStrResult + Mid(sString,i,1)
    Else
    sStrResult = sStrResult + Mid(sString,i,1) + ","
    End If
    Next

    MsgBox sStrResult

    '2) Write a program that mimics the functionality of LCase function. For example, your program should take any variable like “AuTOMatIon123_!@#” and convert all the upper case letter to lower case like “automation123_!@#”.
    'Answer:
    sLcase = "AuTOMatIon123_!@#" 'InputBox("Enter your string value to convert lowercase?")
    msgbox LCase(sLcase)

    '3) Write a program that reverses the order of words (not characters) in a sentence. For example, the program should change the sentence “Good Morning Everybody” to “Everybody Morning Good”
    'Answer:
    sRevWord = "Good Morning Everybody"
    arrRevWord = Split(sRevWord," ")
    arrLen = UBound(arrRevWord)
    For i=arrLen to 0 Step -1
    sReverseWord = sReverseWord + arrRevWord(i) + " "
    Next
    msgbox Trim(sReverseWord)

  • Poovarasan

    '1) Given a string, write a program that finds out all the characters that constitute the string. Example – If the string is VBScript, the output (using a msgbox) should be in this format – The string VBScript contains variables – V, B, S, c, r, i, p, t
    'Answer:
    sString = "VBScript" 'InputBox("Enter your input value?")
    sStrLen = Len(sString)
    For i=1 to sStrLen
    If i=sStrLen Then
    sStrResult = sStrResult + Mid(sString,i,1)
    Else
    sStrResult = sStrResult + Mid(sString,i,1) + ","
    End If
    Next
    MsgBox sStrResult

  • Poovarasan

    '2) Write a program that mimics the functionality of LCase function. For example, your program should take any variable like “AuTOMatIon123_!@#” and convert all the upper case letter to lower case like “automation123_!@#”.
    'Answer:
    sLcase = "AuTOMatIon123_!@#" 'InputBox("Enter your string value to convert lowercase?")
    msgbox LCase(sLcase)

  • Poovarasan

    '3) Write a program that reverses the order of words (not characters) in a sentence. For example, the program should change the sentence “Good Morning Everybody” to “Everybody Morning Good”
    'Answer:
    sRevWord = "Good Morning Everybody"
    arrRevWord = Split(sRevWord," ")
    arrLen = UBound(arrRevWord)
    For i=arrLen to 0 Step -1
    sReverseWord = sReverseWord + arrRevWord(i) + " "
    Next
    msgbox Trim(sReverseWord)

  • AutomationGang

    Really awesome , especially that Real Life Examples was awesome…different from other sites….plz give more examples for the functions in real life examples…

    • Anish10110

      Thanks.. 🙂

  • rajani

    Develop a function that accepts a string of names separated by comma (example given below), add those names in an array and display the names added in the array

    need code without using split

  • amit joshi

    1) Given a string, write a program that finds out all the characters that constitute the string. Example – If the string is VBScript, the output (using a msgbox) should be in this format – The string VBScript contains variables – V, B, S, c, r, i, p, t

    Dim text,arr(20),output

    text="VBScript"

    For i=0 to (Len(text)-1)

    arr(i)=Mid(text,i+1,1)

    if i<(Len(text)-1) Then

    output=output+arr(i)+","

    else

    output=output+arr(i)

    End If

    Next

    MsgBox "The string VBScript contains variables –"+output

  • amit joshi

    Write a program that reverses the order of words (not characters) in a sentence. For example, the program should change the sentence “Good Morning Everybody” to “Everybody Morning Good”

    Dim text,output

    text="Everybody Morning Good"

    arr=Split(text,"")

    For i=UBound(arr) to 0 Step -1

    output=output+arr(i)

    Next

    MsgBox output

  • yogesh

    1. VBScript program

    Str="VBScript"
    strlen=len(Str)
    for i= 1 to strlen
    If i=1 Then
    str2=mid(str,i,1)
    Else
    str2=str2&","&mid(str,i,1)
    End If
    Next
    msgbox str2

    OR

    str="VBScript"
    strlen=len(str)
    For i=1 to strlen
    str2=str2&","&mid(str,i,1)
    Next
    str2len=Len(str2)
    str3=Right(str2,str2len-1)
    msgbox str3

    OR

    • Vikram

      Hi, below number has length of 39, I want to divide that to length 13 and Want to get that values. So I should get 3 values. Can you please help me regarding this.

      000109860025000010986006020001098600763

  • yogesh

    For 2 question below is one of the working code

    str="AuTOMatIon123_!@#"
    strlen=Len(str)
    Dim array1()
    Redim preserve array1(strlen)
    For i=1 to strlen
    array1(i-1)=mid(str,i,1)
    If (array1(i-1)>=chr(65)) and (array1(i-1)<=chr(90)) Then
    array1(i-1)=Lcase(array1(i-1))
    str2=str2&array1(i-1)
    Else
    str2=str2&array1(i-1)
    End If
    Next
    msgbox str2

  • yogesh

    For question 3

    str="Good morning everybody"
    str=Split(str)
    For i=0 to ubound(str)
    str1=str(i)&" "&str1
    Next
    msgbox str1

  • Udayakiran

    Program1–>
    dim vr
    sr= "VBScript"
    l = len(sr)
    ot=""

    For i=1 to l
    ot=ot&","&Mid(sr,i,1)
    Next

    msgbox "VBscript contains letters–"& ot

    ###### Program2 #######

    str2 ="AuTOMatIon123"

    'ASCII range A- Z —> 65 – 90 a-z—> 97 – 122

    l2= len(str2)
    newstr=""

    For i= 1 to l2
    ch= Mid(str2,i,1)
    If (ASC(ch)>=65 AND ASC(ch)<=90) Then
    lch= ASC(ch)+32
    newstr=newstr&Chr(lch)
    Else
    newstr= newstr&ch
    End If
    Next

    msgbox "after converting the string is::"&newstr

    ###### Program 3 ########

    str= "Good Morning Everybody"

    rvrseStr=""

    strary = split(str," ",-1)

    For i=ubound(strary) to lbound(strary) step -1

    rvrseStr = rvrseStr&" "&strary(i)

    Next

    msgbox "Rversed strng:: "&rvrseStr

  • abinash

    how to capture "our" from "welcome to our room" using instr function?

    • Aaditya

      Hey Abinash,
      this is the code
      str="welcome to our room"
      if Instr(str,"our") then
      i=Instr(str,"our")
      msgbox Mid(str,i,3)
      End IF

  • sachin

    String is like "Aa1Bb2$&*" how to seperate lower case words into 1 array,uppercase into another array, numeric values into another and pecial characters into another array ?

  • shaheer

    These are all the functions of VBScript itself.If VBscript runs on QTP, then all the functions of it will, by default work on QTP.

  • Sanj

    dim arrArr(8)

    sText="vbscript"

    iLoop=Len(sText)

    for i=1 to iLoop

    'sChar=mid(sText,i,1)

    arrArr(i)=mid(sText,i,1)

    sString=arrArr(i)

    if i=1 then

    sString1=sString

    else

    sString1=sString1+","+sString

    end if

    next

    msgbox "the string VBscript contains variables-"+sString1

    • Aaditya

      Hi Sanj,
      The same out put can be acheived with this logic also.
      sText="vbscript"
      iLoop=Len(sText)
      for i=1 to iLoop
      str2=str2+Mid(sText,i,1)+","
      Next
      msgbox "the string VBscript contains variables-"+str2

  • Newbie

    q. 1

    str = "VBScript"

    For i = 1 To Len(str)+Len(str) Step 1

    str= Replace(str,left(str,i),left(str,i) & ",")

    i = i+1

    Next

    msgbox str

  • Aaditya

    Hi, Rajani
    Below code is the function for displaying the names added in an array which are seperated by comma in a string.
    Str="Aaditya,Murali,Naidu,Koppulas"
    Function DisplayNames(NameString)
    {
    ln=Len(Str)
    j=1
    n=0
    Dim NArra()
    for i=1 to ln
    Ch=Mid(Str,j,1)
    If(Asc(Ch)<>44) Then
    Name=Name+Ch
    ELse
    n=n+1
    Redim Preserve NArr(n)
    NArr(n)=Name
    Name=""
    End If
    j=j+1
    Next
    For k=LBound(NArr) to UBound(NArr)
    Msgbox NArr(k)
    Next
    } 'End of the Function

  • shashank

    If we have string like that "abcd@$pqrst$$%%12345**" then how to print only Special Characters using vbscript .? any body give me a pgm steps..?

    • sachin

      please let me know it any issue

    • sachin

      str="abcd@$pqrst$$%%12345**"

      Set rgp = New RegExp
      rgp.pattern = "[^a-z0-9]"
      rgp.global=True
      rgp.ignorecase=True
      Set nrgp=rgp.execute(str)
      For i=0 to nrgp.count-1
      print nrgp(i)
      Next

    • sachin

      str="abcd@$pqrst$$%%12345**"

      Set rgp = New RegExp
      rgp.pattern = "[^a-z0-9]"
      rgp.global=True
      rgp.ignorecase=True
      Set nrgp=rgp.execute(str)
      For i=0 to nrgp.count-1
      print nrgp(i).value
      Next

  • Frank Lampard

    Write a Function procedure ToMorseCode in VBScript that takes one string argument and returns a string containing the Morse code equivalent.
    please help me out in this. I hav no clue how to proceed.

  • Its yo boy Peezy

    what code should I use if there is a set of characters and I want to validate the last four characters only, Lets say I have 1234xxxxx and I only want to validate if xxxx is existing in this given set. Thanks in advance.

  • Banu

    q1:

    sText = "VBScript"
    iLen = Len(sText)

    For i=1 to iLen Step 1

    msgbox Mid(sText,i,1)

    Next

    q2:

    sText = "AuTOMatIon123_!@#"

    sText = LCase(sText)

    msgbox sText

    q3:

    sText="Good Morning Everybody"

    arrArray=Split(sText)

    iLen=uBound(arrArray)

    For i=iLen To 0 step -1

    sNew=sNew+ " " + arrArray(i)

    Next

    msgbox sNew

  • Srikanth Dasari

    sMessageFromApplication = Browser("Brw").Page("Pg").WebElement("WebEl").GetROProperty("innertext")

    'Check if the mail is successfully displayed or not

    If InStr(sMessageFromApplication, "Email Successfully Sent to") <> 0 Then

    Reporter.ReportEvent micPass, "Pass", "Email Sent Successfully"

    Else

    Reporter.ReportEvent micFail, "Fail", "Email NOT sent"

    End If

    In Above Case :We have a chance of getting -1,In that cases will it Work?

  • Vivek

    'Str = "VBScript"

    '

    'For i = 1 To len(Str)

    ' temp = mid(Str,i,1)

    ' Msgbox temp

    'Next

    'Lcase

    'Str = "AuTOMatIon123_!@#"

    'NewStr = ""

    'For i = 1 To len(Str)

    ' temp = Mid(Str,i,1)

    ' If Asc(temp)>64 and Asc(temp)<91 Then

    ' temp = Chr(Asc(temp)+32)

    ' End If

    ' NewStr = NewStr+temp

    'Next

    'MsgBox NewStr

    'Reverse Order of Characters

    'str = "Good Morning Everybody"

    'words = Split(str," ")

    'revr = ""

    'For i = Ubound(words) to 0 step -1

    ' revr = revr+" "+words(i)

    'Next

    'Msgbox Trim(revr)

  • vikash

    hi
    for the below string:
    a = "12345+-+-"
    can anybody help in getting the below output
    op = "1-2+3-4+5"

  • Sunil CH

    str = "VBSCRIPT"
    output = ""
    do
    output = output&left(str,1)&","
    str = right(str,len(str)-1)

    Loop while len(str) <> 0
    msgbox(left(output,len(output)-1))

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×