Home
VB.NET
Char Examples
Updated Jun 18, 2024
Dot Net Perls
Char. In VB.NET programs the Char type is useful. Chars are value types—this means they are located in the bytes on the evaluation stack.
Char notes. You can get individual Chars from a String variable. One point of confusion occurs when converting Chars to Integers.
Char.ToLower, ToUpper
String example. Strings are composed of individual characters. With a special syntax, we can get these Chars. You cannot assign Chars at indexes inside a string.
Tip To do that, you would need a Char array or a StringBuilder for a mutable character buffer.
Char Array
StringBuilder
Module Module1 Sub Main() ' String variable. Dim s As String = "Delete" ' Char variable. Dim c As Char = "D"c ' Test first character of String. If c = s(0) Then Console.WriteLine("First character is {0}", c) End If End Sub End Module
First character is D
Char, Integer. In many programming languages, you can cast a Char to an Integer. In the VB.NET language, we must use a built-in conversion function.
Tip To get the ASCII number for a Char value, use Asc or AscW. In this program, there is no difference between the two.
Module Module1 Sub Main() Dim a As Char = "a" Dim b As Char = "b" Dim i As Integer = Asc(a) Dim i2 As Integer = AscW(b) Console.WriteLine(i) Console.WriteLine(i2) End Sub End Module
97 98
Byte size. In VB.NET and C#, Chars are 2 bytes. This means they can accommodate values outside of the ASCII range. But it may also cause more memory usage.
Summary. The Char type is used with the String type. When possible, manipulating and handling Chars is preferable to Strings because they are values, not reference types.
Final notes. Chars use less memory than a one-character String. Despite this, compatibility and correctness are most important in a VB.NET program.
String Remove Punctuation
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jun 18, 2024 (edit link).
Home
Changes
© 2007-2025 Sam Allen