Home
VB.NET
Mid Statement
Updated Dec 5, 2023
Dot Net Perls
Mid. This is a special statement in VB.NET that changes, or returns, a part of a String (a substring). The Mid statement is useful in string manipulation.
Mid benefits. Mid() makes String instances easier to mutate. We do not need to use Functions such as Substring. This statement can lead to clearer, shorter programs.
Example. The Mid statement is used with positional arguments. In this example, we change the part of the String from the index 2 with length 2. A new String is returned.
Next We show the Mid function, which simply returns a Substring of the String based on the positional arguments.
Detail The first argument to Mid is the string. The second and third arguments are the start index, and the length of the substring.
Module Module1 Sub Main() ' Input string. Dim value As String = "bird" ' Replace part of string with another string. Mid(value, 2, 2) = "an" ' Write. Console.WriteLine(value) ' Get middle part of string. Dim m As String = Mid(value, 2, 2) Console.WriteLine(m) End Sub End Module
band an
Implementation. Mid is compiled into the StringType.MidStmtStr subroutine, which internally validates the arguments and then uses a StringBuilder to build up the result.
StringBuilder
Detail What about the Mid function? This one is implemented in the obvious way: with the Substring function on the String type.
String Substring
When programming in .NET it is usually better to use the methods that are available to both VB.NET and C# environments. This makes program logic more portable and understandable.
Summary. Mid() in VB.NET can be useful when you want to mutate a part of a String or get a substring. The Mid statement does not make a string mutable—it creates a new string copy.
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 Dec 5, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen