Tag Archives: chr

Using Chr() Code to Add Quotes to a Value in Visual Basic and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 4.33 out of 5)
 

At one time or another you may want to use quote characters ” ” as a value.  Since VB views quotes as containing the value, you would need to add multiple quotes around quotes to add quotes (huh?) to be a value. To me doing multiple quotes like that can be alittle tricky and look cluttered. Below is a example to add quotes around a string value doing it the multiple quotes way…

Dim str As String

str = """Quotes!"""

There is a better/easier way to do this (at least to me 🙂 ). It is a simple little feature that is available in All versions of Visual Basic and Visual Basic.NET, the Chr(ChrCode) Function.

Public Function Chr(ByVal CharCode As Integer) As Char

This little function will return the specified ChrCode Integer as its mapped character value. Chr$ coding is very easy to do. The codes below will add literal quotes to a string value…


Dim str AsString 

str = Chr(34) & "Quotes!" & Chr(34)

To me, the Chr version looks better and is easier to read. It would be more apparent as you concatenate (link) strings together with quotes. There are many more scenerios for Chr like using ChrCode #13 which will mimick the Enter/Return key. I may going over Chr Code more later on. Have fun 🙂

 

Jason