Skip to content
AgileLogics
5 min read

String Methods and Formatting

Prerequisites: Strings

Everyday methods

Module Program
    Sub Main()
        Dim text = "  The quick brown fox  "
        Dim clean = text.Trim()

        Console.WriteLine($"[{clean}]")
        Console.WriteLine(clean.Contains("quick"))
        Console.WriteLine(clean.StartsWith("The"))
        Console.WriteLine(clean.IndexOf("brown"))
        Console.WriteLine(clean.Replace("quick", "slow"))
        Console.WriteLine(clean.ToLower())
        Console.WriteLine("7".PadLeft(3, "0"c))
        Console.WriteLine("abc".PadRight(6, "."c) & "|")
    End Sub
End Module
[The quick brown fox]
True
True
10
The slow brown fox
the quick brown fox
007
abc...|

IndexOf returns the zero-based position, or -1 if the text isn't found.

Split and Join

Module Program
    Sub Main()
        Dim csv = "Ada,36,London"
        Dim parts = csv.Split(","c)
        Console.WriteLine(parts.Length)
        Console.WriteLine(parts(2))

        Dim words = "one  two three".Split(" "c, StringSplitOptions.RemoveEmptyEntries)
        Console.WriteLine(String.Join(" | ", words))

        Dim letters = "hello".ToCharArray()
        Array.Reverse(letters)
        Console.WriteLine(New String(letters))
    End Sub
End Module
3
London
one | two | three
olleh

Number and date formats

Imports System.Globalization

Module Program
    Sub Main()
        Dim us = CultureInfo.GetCultureInfo("en-US")
        Dim amount = 1234567.891D

        Console.WriteLine(amount.ToString("N2", us))
        Console.WriteLine(amount.ToString("C", us))
        Console.WriteLine(0.256.ToString("P1", us))
        Console.WriteLine(amount.ToString("F0", us))
        Console.WriteLine(String.Format(us, "{0} owes {1:C}", "Ada", 42.5D))

        Dim d = New Date(2025, 3, 9, 14, 30, 0)
        Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm"))
        Console.WriteLine(d.ToString("dddd, MMMM d", us))
    End Sub
End Module
1,234,567.89
$1,234,567.89
25.6%
1234568
Ada owes $42.50
2025-03-09 14:30
Sunday, March 9
Format Meaning
N2 number with thousands separators, 2 decimals
C currency
P1 percentage, 1 decimal
F0 fixed point, no decimals
D5 integer padded to 5 digits

Formats follow the culture (regional settings): the same amount is $1,234,567.89 in en-US and 1.234.567,89 € in de-DE. Passing a culture, as above, gives the same output on every computer.

Legacy string functions

VB also keeps VB6-style functions in the Microsoft.VisualBasic namespace, which you'll see in older code:

Module Program
    Sub Main()
        Dim s = "Visual Basic"
        Console.WriteLine(Len(s))
        Console.WriteLine(Left(s, 6))
        Console.WriteLine(Right(s, 5))
        Console.WriteLine(Mid(s, 8, 3))
        Console.WriteLine(UCase(s))
        Console.WriteLine(InStr(s, "Basic"))
    End Sub
End Module
12
Visual
Basic
Bas
VISUAL BASIC
8

Note that Mid and InStr count from 1, while Substring and IndexOf count from 0. Prefer the .NET methods in new code; recognize the old ones when you maintain existing code.