Skip to content
Lesson 7 of 7

Step 1 of 5 · Reading · ~3 min

Learn

Subs, Functions, Arrays

VB.NET strings are .NET String objects — the same type C# uses — plus VB-specific functions like Mid, Left and Right inherited from VB6.

Concatenation

Dim s As String = "hello" & " " & "world"   ' & is the canonical concat
s &= "!"                                     ' append

+ also joins two strings, but it stays the addition operator, so it is ambiguous the moment an operand might be a number. & never is.

Length and indexing

Dim s As String = "hello"
Console.WriteLine(s.Length)          ' 5
Console.WriteLine(s.Substring(1, 3)) ' "ell"  (start, length)
Console.WriteLine(s.Substring(2))    ' "llo"  (from index to end)

s(0) is a Char, not a one-character StringTypeName(s(0)) reports Char. That distinction bites when you concatenate: Char.ToUpper(s(0)) & s.Substring(1) works because & converts, but a method expecting a String will not take the Char directly.

Strings are immutable. s &= "!" does not extend anything; it allocates a new string and rebinds s.

Common methods

s.ToUpper()               ' "HELLO"
s.Trim()
s.IndexOf("l")            ' 2  — and -1 when not found, never an error
s.Contains("ell")         ' True
s.StartsWith("hello")
s.Replace("l", "L")       ' "heLLo"
s.Split(" "c)             ' array of substrings; the c suffix makes a Char literal
String.Join(",", New String() {"a", "b", "c"})    ' "a,b,c"

Split(" "c) on a line with two spaces in a row yields an empty string between them — worth remembering before you index w(0) on every piece.

Legacy VB6-style functions

Still available, mostly for compatibility:

Mid(s, 2, 3)              ' "ell"   — 1-INDEXED
Left(s, 3)                ' "hel"
Right(s, 2)               ' "lo"
Len(s)                    ' 5
UCase(s)                  ' "HELLO"

Mid starts at 1; Substring starts at 0. Pick one style per file.

Formatting

Dim name As String = "Ada"
Dim age As Integer = 36
Console.WriteLine(String.Format("{0} is {1}", name, age))   ' Ada is 36
Console.WriteLine(String.Format("[{0,-6}]", name))          ' [Ada   ]
Console.WriteLine(String.Format("{0:N2}", 1234.567))        ' 1,234.57

{0,-6} pads to width 6, left-aligned (a positive width right-aligns). Format specifiers after the colon: N (number with separators), C (currency, locale-dependent), P (percent), F2 (2 decimals), X (hex).

Newer VB writes the same thing as an interpolated string, $"{name} is {age}". That syntax arrived in VB 14 (2015); the compiler behind this course predates it and rejects $"...", so String.Format and & are what work here.

Building strings in a loop

Imports System.Text

Dim sb As New StringBuilder()
For i As Integer = 1 To 1000
    sb.Append(i.ToString())
    sb.Append(","c)
Next
Dim result As String = sb.ToString()

Because strings are immutable, result &= piece in a loop allocates a fresh string on every pass — quadratic work. StringBuilder keeps one growable buffer and copies once at .ToString().

Your exercise

Split the line, then rebuild it with each word's first letter upper-cased: Char.ToUpper(w(0)) & w.Substring(1). Append into the StringBuilder the starter gives you, adding a single space between words — the guard If i > 0 is already there so you do not emit a leading space. The grader compares the printed line after trimming trailing whitespace, but a leading or interior space is a real difference.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…