Friday, 30 August 2013

String And Array Manipulations in VB Scripting: Part2: String Function

Most Useful String Functions:

Hello folks,, I will be discussing few string function which are very useful in our day to day automation coding.
1. InStr([start, ]string1, string2[, compare])
most use variant of this function is as below
This function searches the occurrence of the str2 in the string str1, and returns the first occurrence of the str2.

str1="This world is good world"
msgbox Instr(str1,str2)

:OutPut 6
Notice that it is the position not the index or the subscript as one can confuse it with arrays in which the base is 0.
Now the more advanced version

str1="This world is good world"
msgbox Instr(7,str1,str2)
:output 20
Here the first parameter '7' is optional, it shows the starting point of the search.So the first occurrence of "world " is ignored....
The second optional parameter  in below is for comparison mode(0,1).'0 ' means the exact matching(case sensitive, binary comp), '1' means textual comp(case insensitive,textual comp)

str1="This world is good world"
msgbox Instr(7,str1,str2,0)
msgbox Instr(7,str1,str2,1) 
:output 0
:ouput 20
This completes the Instr Function 

2. InStrRev([start, ]string1, string2[, compare]): Identical to Instr but the search is reversed it search the string from the right side, rest all is same.

Hope it helped..

3.Mid(string, start[, length]): It is one of the very very very important functions in VbScripting.
it gives any specific part of the string .


now i want to extract the second letter in it. As it is not a array so we cant use str(1).
So we use

msgbox str2

Output: e 
Now in this statement
Instructs the VB engine that it has to start from the second element(2 as second parameter) and extract the 1 element(1 as the third parameter) from the string str1(first parameter).

This function is used mostly in reversal of string without using reverse function.
see post.
Hope this helped

No comments:

Post a Comment