Strings and Text Processing
9.1 String Basics
Strings are immutable System.String objects. Use StringBuilder for repeated modifications.
Dim s As String = "Hello, World"
Console.WriteLine(s.Length) ' 12
Console.WriteLine(s(0)) ' "H"c (Chars indexer)
Console.WriteLine(s.Substring(7)) ' "World"
Console.WriteLine(s.Substring(0, 5)) ' "Hello"
Dim empty = String.Empty
Console.WriteLine(String.IsNullOrEmpty(s), String.IsNullOrWhiteSpace(" "))9.2 Common String Methods
Dim text = " Visual Basic .NET "
text.Trim() ' "Visual Basic .NET"
text.TrimStart() : text.TrimEnd()
text.ToUpper() : text.ToLower()
text.Contains("Basic") ' True
text.StartsWith(" Vis") : text.EndsWith("T ")
text.IndexOf("Basic") ' 9
text.LastIndexOf("a")
text.Replace("NET", "Core")
text.Insert(0, ">>") : text.Remove(0, 2)
text.PadLeft(30, "*"c) : text.PadRight(30)
"a,b,c".Split(","c) ' {"a", "b", "c"}
"a, b;c".Split({",", ";"}, StringSplitOptions.RemoveEmptyEntries Or StringSplitOptions.TrimEntries)
String.Join("-", {"2025", "10", "24"})
String.Concat("a", "b", "c")
"abc".ToCharArray()
New String("*"c, 10)
String.Compare("a", "B", StringComparison.OrdinalIgnoreCase)
"Hello".Equals("hello", StringComparison.OrdinalIgnoreCase)9.3 String Formatting and Interpolation
Number with separators
Dim name = "Ana", balance = 1234.567D, dt = Date.Now
Console.WriteLine($"Hello {name}, balance: {balance:C2}, date: {dt:yyyy-MM-dd}")
Console.WriteLine($"{name,-10}|{balance,12:N2}|") ' alignment: left -10, right 12
Console.WriteLine(String.Format("{0} has {1:P1} share", name, 0.256))
Console.WriteLine(balance.ToString("#,##0.00"))
Console.WriteLine(255.ToString("X")) ' FF (hex)
Console.WriteLine(42.ToString("D5")) ' 00042
Console.WriteLine($"Braces: {{literal}}")9.4 StringBuilder
Imports System.Text
Dim sb As New StringBuilder()
For i = 1 To 5
sb.Append("Item ").Append(i).AppendLine()
Next
sb.Insert(0, "List:" & vbCrLf)
sb.Replace("Item", "Entry")
Dim result = sb.ToString()9.5 Legacy VB String Functions (Microsoft.VisualBasic)
Left(s, n) / Right(s, n) s.Substring(0, n) / s.Substring(s.Length - n) Mid(s, start, len) (1-based) s.Substring(start - 1, len)
InStr(s, find) (1-based, 0 if not found) s.IndexOf(find) + 1 UCase / LCase ToUpper / ToLower Trim / LTrim / RTrim Trim / TrimStart / TrimEnd Replace(s, a, b) s.Replace(a, b)
StrReverse or New String(s.Reverse().ToArray()) Asc / Chr / AscW / ChrW Convert.ToInt32(c) / ChrW(n) Parses the leading numeric part (42) Format(x, "0.00") vbCrLf, vbTab, vbNewLine
Environment.NewLine, ControlChars.Tab These still work (and are fine in VB), but.NET methods are preferred for consistency and 0-based indexing.
9.6 Characters and Regular Expressions
Char.IsDigit("5"c) : Char.IsLetter("a"c) : Char.IsWhiteSpace(" "c) : Char.ToUpper("a"c)
Imports System.Text.RegularExpressions
Dim email = "[email protected]"
If Regex.IsMatch(email, "^[\w.+-]+@[\w-]+\.[\w.]+$") Then Console.WriteLine("Valid")
Dim m = Regex.Match("Order 12345 shipped", "\d+")
Console.WriteLine(m.Value) ' 12345
For Each match As Match In Regex.Matches("a1b22c333", "\d+")
Console.WriteLine(match.Value)
Next
Dim cleaned = Regex.Replace("Phone: 98-765-43210", "[^\d]", "")
Dim parts = Regex.Split("one1two2three", "\d")
Dim dateMatch = Regex.Match("2025-10-24", "(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})")
Console.WriteLine(dateMatch.Groups("year").Value)