Strings
Prerequisites: Data types
A String is a sequence of characters. Strings are immutable:
methods like ToUpper return a new string and leave the original alone.
Module Program
Sub Main()
Dim first = "Grace"
Dim last = "Hopper"
Dim full = first & " " & last
Console.WriteLine(full)
Console.WriteLine(full.Length)
Console.WriteLine(full(0))
Console.WriteLine(full.Substring(0, 5))
Console.WriteLine(full.ToUpper())
Console.WriteLine(full)
End Sub
End Module
Grace Hopper
12
G
Grace
GRACE HOPPER
Grace Hopper
&joins strings.+also works on two strings, but with numbers it adds instead, so always use&for text.full(0)is the first character — indexes start at 0.Substring(start, length)takes part of a string.
Quotes and special characters
VB strings have no backslash escapes. A double quote inside a string is written as two double quotes, and special characters come from constants:
Module Program
Sub Main()
Console.WriteLine("She said ""hello"".")
Console.WriteLine("C:\Users\ada\notes.txt")
Console.WriteLine("Line one" & vbCrLf & "Line two")
Console.WriteLine("Name:" & vbTab & "Ada")
Console.WriteLine("Also works:" & Environment.NewLine & "next line")
End Sub
End Module
She said "hello".
C:\Users\ada\notes.txt
Line one
Line two
Name: Ada
Also works:
next line
vbCrLf, vbLf, and vbTab are VB constants; Environment.NewLine
is the platform's line ending. Backslashes are ordinary characters, which
makes Windows paths easy.
Multi-line strings
Since VB 14, a string literal can span several lines:
Module Program
Sub Main()
Dim poem = "Roses are red,
Visual Basic is readable."
Console.WriteLine(poem)
End Sub
End Module
Roses are red,
Visual Basic is readable.
Interpolated strings
Module Program
Sub Main()
Dim item = "coffee"
Dim price = 3.5D
Dim qty = 3
Console.WriteLine($"{qty} x {item} = {price * qty}")
Console.WriteLine($"Total: {price * qty:F2}")
Console.WriteLine($"Left |{item,-8}| Right |{qty,5}|")
Console.WriteLine($"Braces: {{like this}}")
End Sub
End Module
3 x coffee = 10.5
Total: 10.50
Left |coffee | Right | 3|
Braces: {like this}
Inside $"...", {expr:format} applies a format (F2 = two decimals)
and {expr,width} pads to a width (negative means left-aligned). Double
the braces to print them literally.
Comparing strings
Module Program
Sub Main()
Console.WriteLine("apple" = "apple")
Console.WriteLine("Apple" = "apple")
Console.WriteLine(String.Equals("Apple", "apple", StringComparison.OrdinalIgnoreCase))
Console.WriteLine("apple" < "banana")
Console.WriteLine(String.IsNullOrEmpty(""))
End Sub
End Module
True
False
True
True
True
The language is case-insensitive, but string values are compared
case-sensitively by default (Option Compare Binary). Use
StringComparison.OrdinalIgnoreCase when case shouldn't matter.
Practice
Store a first and last name in two variables and print the initials
(like G.H.) and the full name in upper case, using an interpolated
string.
Show solution
Module Program
Sub Main()
Dim first = "Grace"
Dim last = "Hopper"
Console.WriteLine($"{first(0)}.{last(0)}. {(first & " " & last).ToUpper()}")
End Sub
End Module
G.H. GRACE HOPPER