Review
BEST Laptop for Hacking in 2021
Ethical Hackers need to run software like password cracking tools, virtual machines, Kali Linux to...
The substring function is used to obtain a part of a specified string. This method is defined in the String class of Microsoft VB.NET. You have to specify the start index from which the String will be extracted. The String will be extracted from that index up to the length that you specify.
In this tutorial, you will learn
The function accepts two arguments as shown in the following syntax:
Public Function Substring(ByVal start_Index As Integer, ByVal sub_length As Integer) As String
Here,
Step 1) Create a new console application.
Step 2) Add the following code to it:
Module Module1
Sub Main()
Dim st As String = "gtupapers"
Dim subst As String = st.Substring(0, 4)
Console.WriteLine("The substring is: {0}", subst)
Console.ReadKey()
End Sub
End Module
Step 3) Click the Start button from the toolbar to execute the code. You should get the following result:
We have used the following code:
Explanation of Code:
What if we pass only one argument to the function? The function will copy all the data in the String that begins from that index. What happens is that the Substring function internally copies all the string data at that index as well as that which follows that index. For example:
Module Module1
Sub Main()
Dim st As String = "gtupapers"
Dim subst As String = st.Substring(4)
Console.WriteLine("The substring is: {0}", subst)
Console.ReadKey()
End Sub
End Module
Click the Start button to run the code. It should return the following:
The substring function returned 99. We passed the parameter 4 to the function, meaning that it will begin to extract the substring from the character at index 4 to the end of the String. 9 is the character at index 4 of the string gtupapers, hence the extraction started there.
It is also possible for us to get the middle characters of the String in question. In this case, we only have to provide the starting index and the length of the String that we need. In the following example, we are getting a substring of the specified String from index 2 and the String will have a length of 2 characters:
Module Module1
Sub Main()
Dim st As String = "gtupapers"
Dim subst As String = st.Substring(2, 2)
Console.WriteLine("The substring is: {0}", subst)
Console.ReadKey()
End Sub
End Module
Click the Start button from the toolbar to run the code. You will get the following result:
In the above example, the substring function returned ru. We passed the parameters (2, 2) to the function. The first 2 instructs the function to begin the extraction of the substring from index 2 while the second 2 instructs the function to return a substring with a length of 2 characters only. This means that the extraction of the substring should begin from the element located at index 2 of the string gtupapers, which is r. Since the returned substring should only have a length of 2 characters, the extraction will not go past the 'u', hence it returned 'ru.'
We can use the Substring function to get a single character from a string. In such a case, it is a necessity for you to make an allocation but the character can be accessed directly. This is a bit faster. The following example demonstrates two ways through which we can achieve this:
Module Module1
Sub Main()
Dim st As String = "gtupapers"
Dim mid1 As Char = st(1)
Console.WriteLine(mid1)
Dim mid2 As String = st.Substring(1, 1)
Console.WriteLine(mid2)
Console.ReadKey()
End Sub
End Module
Click the Start button to run the code. You will get the following result:
We have used the following code:
Explanation of Code:
Ethical Hackers need to run software like password cracking tools, virtual machines, Kali Linux to...
Professional programmers understand the benefits of having the best monitor for programming. A...
What is Prototyping Model? Prototyping Model is a software development model in which prototype is...
Following are frequently asked questions in interviews for freshers as well as experienced Java...
What is Jenkins? Jenkins is an open source Continuous Integration server capable of orchestrating a...
Download PDF 1) Explain what DevOps is? It is a newly emerging term in the IT field, which is...