In this article, am going to discuss with you everything you need to know about Visual Basic Substring as well as give you detailed examples
In programming, substring function is used to obtain a part of a specified string from a given expression or string.
It is part of the Visual Basic String class. In order for substring to work perfectly, you have to specify the start index from which the extraction will start and the length it should take. The String will be extracted from that index up to the length that you specify.
In this tutorial, you will learn
What is a SubString?
Syntax of Substring
Examples
One Argument
Middle Characters
One Char
String.Substring(StartIndex, Length)
The function accepts two arguments as shown in the above syntax
Parameters
-
Start index: Is the starting index from which the string extraction will start.
- Length: The length denote the total characters that will be returned by the substring function
Let us see an example of substring demonstration. The example below is a console application demonstrating visual basic substring.
Module substringTest
Sub Main()
Dim str As String = "I love Emmason247.com.ng"
Dim res As String = str.Substring(7, 17)
Console.WriteLine("The substring is: {0}", res)
Console.ReadKey()
End Sub
End Module
Output
Emmason247.com.ng
Run the above example an your result will be "Emmason247.com.ng"
Why you seen this result is because you we specified the start index to be 7 which is the length of "I love " and the length to be 17 which is the length of "Emmason247.com.ng"
Let us try another example
Module substringTest
Sub Main()
Dim str As String = "I love Emmason247.com.ng"
Dim res As String = str.Substring(0, 6)
Console.WriteLine("The substring is: {0}", res)
Console.ReadKey()
End Sub
End Module
Output
I love