HomeSearch

VB.NET Mid Statement

Use the Mid Function to handle substrings. Review the arguments of Mid.
Mid changes a part of a String. It can also return a substring. The Mid statement is particularly useful in VB.NET programs. It makes String instances easier to mutate. We do not need to use Functions such as Substring.
Example. The Mid statement can be used with the name of the variable string you want to change, and positional arguments. In this example, we change the part of the String from the index 5 to the index 8 (length 3). A new String is returned.

Next: We show the Mid function, which simply returns a Substring of the String based on the positional arguments.

VB.NET program that uses Mid statement, function Module Module1 Sub Main() ' Input string. Dim value As String = "Dot Net Perls" ' Replace part of string with another string. Mid(value, 5, 3) = "Cat" ' Write. Console.WriteLine(value) ' Get middle part of string. Dim m As String = Mid(value, 5, 3) Console.WriteLine(m) End Sub End Module Output Dot Cat Perls Cat
Implementation. How is the Mid statement implemented? Mid is compiled into the StringType.MidStmtStr subroutine, which internally validates the arguments and then uses a StringBuilder to build up the result.StringBuilder

Function: What about the Mid function? This one is implemented in the obvious way: with the Substring function on the String type.

Substring
As a general rule, when programming in the .NET Framework it is better to use the methods that are readily available to both VB.NET and C# environments. This makes program logic more portable and also understandable for others.
Summary. The Mid statement and Mid function 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 provides the appearance of mutation but creates a new copy.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.