Home
VB.NET
Mid Statement
This page was last reviewed on 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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 5, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.