Microsoft & .NETVisual BasicString Functions Explained

String Functions Explained content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Visual Basic provides several functions to format and retrieve parts of

Joining strings with &

You can use the & operator to join two strings. This operation is
called concatenation. For example:

FirstString = "This string has "
SecondString = "been concatenated!"
NewString = FirstString & SecondString

NewString would contain "This string has been concatenated!".
Note that you need a space at the end of the first string or at the beginning of the
second string, so that there is a space between "has" and "been".

Left$, Right$

These functions retrieve part of the string. The Left$ function returns
characters from the left hand side, and the Right$ function returns characters from the
right hand side. This is what they do:

MyString = "This is a nice long string"
LeftString = Left$(MyString, 7)
RightString = Right$(MyString, 6)

LeftString contains the text "This is", and RightString contains
the text "string".


Left$(string, length)
Right$(string, length)


This function is used to retrieve part of a string, much like Left$ and
Right$. This is how it is used:

MyString = "This is a nice long string"
NewString = Mid$(MyString, 6, 21)

NewString now contains "is a nice long string"


Mid(string, start[, length])

UCase$, LCase$

Use these two functions to convert a string to entirely upper case or
entirely lower case characters. This is very useful if you want to do a case insensitive
search, or if a password is not case sensitive. For example:

MyString = "This is a nice long string"
UCaseString = UCase$(MyString)
LCaseString = LCase$(MyString)

UCaseString contains "THIS IS A NICE LONG STRING", and
LCaseString contains "this is a nice long string". This can be used practically:

PasswordAttempt = "MyPaSs"
ActualPassword = "mYpAsS"If PasswordAttempt = _
      ActualPassword Then succeeded1 = True 
   succeeded1 = False
End If

If UCase$(PasswordAttempt) = UCase$(ActualPassword) _
   Then succeeded2 = True 
   succeeded2 = False
End If

In this case, succeeded1 would be False, and suceeded2 would be True. This
is because the two actual passwords do not equal each other, but when they are both
converted into upper case, they do equal each other.


Mid(string, start[, length])


This function returns a string of repeating characters. For example,
String$(20, "*"), would return "********************".


String(number, character)


This function returns the length of a string. It is useful in a For…Next
loop to do something to each character in a string. For example:

mystring = "Hello world!"
mylen = Len(mystring)
For charnum = 1 To mylen
Debug.Print Mid$(mystring, charnum, 1)

Here you will get H, e, l, l, o, , w, o, r, l, d, !, printed out in the
debug widow, with each character on a separate line. mylen returns 11 because the length
of the string is 11.



Watch out soon for advanced string
, where we will show you how to do search and replace facilities, and look at
the other string functions. 

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories