String functions in vbscript
Below is the list of string functions in vb script
Examples -
Syntax -
Second parameter in left and right function tells how many characters to return from the string.
In mid functions there are 2 parameters. First parameter tells from which position of the string we have to get the characters and second parameter tells how many characters to return.
Example -
There are 3 functions in this category.
Example -
msgbox rtrim(mainString)
The syntax of the StrComp function is ->
- lcase
- ucase
- len
- left
- right
- mid
- ltrim
- rtrim
- trim
- replace
- strreverse
- string
- Instr
- Instrrev
- strcomp
We are going to have a look at each String function mentioned above.
Substring Extraction functions
Some of the commonly used string manipulation functions are given below.- right
- mid
- left
All of the above functions are frequently used when performing any string operations in vbscript.
All of the above functions extract the part of the string / Sub string.
Right function returns the fixed number of characters from right side of the string.
Left function returns the fixed number of characters from left side of the string.
Mid function can be used to get the characters/ sub string from the left, right or middle part of the string.
Examples -
myString = "Sachin Plays Cricket" print right(myString,7) 'will return the 7 characters from the right side of myString 'Cricket print left(myString,6) 'will return the 6 characters from the left side of myString 'Sachin print mid(myString,8,5) 'will return the 5 characters from the 8th position of myString 'Plays
Syntax -
Second parameter in left and right function tells how many characters to return from the string.
In mid functions there are 2 parameters. First parameter tells from which position of the string we have to get the characters and second parameter tells how many characters to return.
Converting Case of Strings
We can convert the string from lower case to upper case and vice versa using ucase and lcase functions in VBScript.str = "We are learning strings in VBScript" Msgbox lcase(str) 'It will print - we are learning strings in vbscript Msgbox ucase(str)'It will print - WE ARE LEARNING STRINGS IN VBSCRIPT
Replacing part of the string
Replace function can be used to replace the part of the string with other string. Syntax of replace is given below.
Replace(mainString,stringToFind,replaceString[,start_Index[,Replace_count[,comparison_mode]]])
More information on the parameters is given below.
- mainString - This is the original string
- stringToFind - This is the string which will be searched and replaced
- replaceString - This is the string which will replace other string in the original string
- start_Index - Index position of the original string from where you have to search it
- Replace_Count - How many occurrences of the string you want to replace
- Comparison_Mode - Binary (Case Sensitive) or textual comparison (Case Insensitive). by default it is binary comparison.
Examples -
mainString = "Sachin plays cricket" msgbox replace(mainString,"Sachin","Arjun") 'Prints Arjun plays cricket.
Finding the length of the string
Len function is used to find the length of the string.Example -
mainString = "Sachin plays cricket" msgbox len(mainString) 'Prints 20
Trim functions of the string
Trim functions are used to remove the blank spaces from the beginning and ending of the string.There are 3 functions in this category.
- ltrim - removes blank spaces from left side of the string
- rtrim - removes blank spaces from right side of the string
- trim - removes blank spaces from both left and right side of the string
Example -
mainString = " Sachin plays cricket " msgbox ltrim(mainString) 'Prints "Sachin plays cricket "
msgbox rtrim(mainString)
'Prints " Sachin plays cricket" msgbox trim(mainString) 'Prints "Sachin plays cricket"
Reverse the string in VBScript
We can use strreverse function to reverse the string in VBScript.
Example -
mainString = "Sachin" msgbox strreverse(mainString) 'Prints "nihcaS"
String function in VBScript
We can use string function to get the specified character n times.
Example -
msgbox String(5,"*") 'prints *****
Instr function in VBScript
We can use instr function to find the substring in given string. Searching happens from the beginning of the string.
Syntax of Instr
InStr([start,]string1,string2[,compare]) msgbox instr(1,"sagaar","g") 'prints 3
Instrrev function in VBScript
We can use instrrev function to find the substring in given string. Searching happens from the end of the string but the position of the character is counted from the beginning of the string.
Syntax of Instrrev
InStrRev(string1,string2[,start[,compare]]) msgbox instrrev("sagaar","g") 'prints 3
Comparing 2 strings in VBScript
We can use StrComp function to compare two strings.
The StrComp function returns the values based upon comparison result.
- -1 (if string1 < string2)
- 0 (if string1 = string2)
- 1 (if string1 > string2)
- Null (if string1 or string2 is Null)
StrComp (string1, string2 [, comparison_mode])
The last parameter determines whether comparison is binary or textual. By default it is binary comparison (Case Sensitive).
msgbox strcomp("Amol","Sagar") 'prints -1..since ascii value of A is less than that of S msgbox strcomp("sagar","sagar") 'prints 0 msgbox strcomp("sagar","Amol") 'prints 1
0 comments:
Post a Comment