String Functions

Numbers to Strings

To convert any number to a string, you can use the Str Function. The Str Function uses the following syntax

Str$(Number)

**** Note the $ that has been appended to the Str function name (and many other string functions) You do not have to do this, but if you don't, it will return a Variant rather than a string... which is another VB behavour I can't understand, as we are specifically wanting a string! VB will automatically convert this to a string, but at a performance cost (not noticable if you just do it once, but it is a good habit to get into!). You can see our Performance article for more info. ***

The following code converts the number contained in lNum, and puts it into a textbox

Dim lNum As Long
lNum = 1000
Text1.Text = Str$(lNum)

In many cases it is not necessary to convert a number to a string, as VB automatically does this for you, however it is a good practice to get into, and it is also slightly faster using the Str function, instead of letting VB do it for you. You can also use the Hex command to convert a number to a Hex string. To do this properly, use the following code. This makes all the letters lowercase, and then adds &H on to the beginning, making it a valid Hex string

strHexString = "H&" & LCase$(Hex$(lNumber))

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup