String Functions - posted 10th April 2004 ( 0 comments )

Nearly all your VB projects will involve a string function at some point in it's creation.

Format
If you need to control the display features (such as number of decimal places) you use the Format function
This function has two arguments, the first is the string to format, the second is how to display the string.
As an example, to display NUMBERS with no more than two decimal points, use:
NUMBERS = 2.4531983394
txtDemo.Text = Format(Numbers, "#.##")
This would give us 2.45

Left
The Left function is used to extract a certain amount of characters from the left of the string. For example : -
WebSite = "www.carbonize.co.uk"
Leftstring = Left(WebSite, 5)

This makes Leftstring equal to "www.c"

Right
The Right works the same way as the Left function but extracts the characters from the right of the string. For example :-
WebSite = "www.carbonize.co.uk"
Rightstring = Right(WebSite, 7)

This makes Rightstring equal to "e.co.uk"

Mid
The Mid function is used to extract characters from anywhere in a string. You just define the starting point and the number of characters to extract. For example :-
WebSite = "www.carbonize.co.uk"
MidString = Mid(WebSite, 5, 9)

This makes Midstring equal to "carbonize"

Len
The Len function is used for determining the length of a string. For example :-
WebSite = "www.carbonize.co.uk"
LenString = Len(WebSite)

This make LenString have a value of 19

Instr
The Instr function is to locate a substring within a string. It uses 3 arguments to achieve this. Argument 1 is the point in the string to start looking from, Argument 2 is the name of the string to look through, and Argument 3 is the substring to locate. For example :-
WebSite = "www.carbonize.co.uk"
Location = Instr(4, WebSite, "oni")

This will look through the string 'WebSite', starting at character 4, for the substring "oni". If the substring is located the program will return the value which is the position of the first character of the substring, so in our example Location would equal 9, which is the position of the "o" in "oni".

Replace
replace is used when you want to replace all occurences of a substring in a string with a different substring. The syntax is replace(STRING, "REPLACETHIS", "WITHTHIS"). This will go through the string called string and replace all occurences of REPLACETHIS with WITHTHIS.
example :-
replace("c#a#r#b#o#n#i#z#e#", "#", "")
This will output carbonize.

Split
This is used when we want to split a string up into several items. It's mostly used getting data from lists that are in a single line with This is used to split up items from a string by giving the marker or delimiter is used to seperate the seperate items.
example :-
blah = "a,b,c,d"
split(blah, ",")
This would give us
a
b
c
d

Next we look at a pair of very useful functions, Asc and Chr.
Every ´typeable´ character has a numeric representation called an ASCII ("askey") code. The Asc function returns the ASCII code for an individual character. For example :-
Asc("A")
Will give you the ASCII code for "A" which is "65"
The Chr function works the other way around. Chr will return character respresented by an ASCII code. For example :-
Chr(75)
This returns the character represented by the ASCII code "75", Which is "K".

Lcase
This is a very useful command to know. The Lcase command will convert all the letters in a string to lowercase (eg. 'Carbonize SUCKS!!' becomes 'carbonize sucks!!')
The format for using this command is Lcase(string)

Ucase This is like Lcase except it converts all letters to uppercase.



Concerned about phishing or want to know more about it? Then read our fake logins page.