Updated March 13, 2023
Introduction to VB.Net String Functions
A string is nothing but a collection of characters. In very simple terms, String may be defined as the array of characters. When it comes to an understanding variable, Integer is the very first thing we learn about. An integer is the data type that stores the integer value, in the same way, char is the data type that stores single character and similarly, a string is the data type that allows the storage of the set of characters in a single variable.
We can also store the string in a char array but the simple way to store any string is by making the use of string data type. We can perform several functions on the strings that we will see later. The ability to process string values helps a lot to create complex applications. Now in the next section, we will learn how to implement string.
Declaration and Initialization of VB.Net
To bring string in actual use, we will first need to declare the string. Once it is declared we can use it numerous of times as required. Below is the syntax to declare a string in VB .net.
Dim Str as String
- Dim: It is the keyword used while declaring any variable.
- Str: It is the name of the variable that will hold the string value.
- String: It is the keyword that is used to state that the value that Str will hold should be a string.
Now, once the name of the variable is declared we have to put some value in it so that it could be used in the program. We can assign the value to the variable either by taking input from a user in run time or we can assign the value manually. Here we will see how we can assign the value manually.
Str="Latin"
Here the value has been assigned to the variable str. While assigning the string value to the variable we have to make sure that the values have to be written in double quotes. Once the values are assigned, we can use it anywhere in the program.
Working with VB.Net String Functions
In this section, we will see how string could be used with several functions in order to process the value and generate the desired output. Below are the string functions that are used to work with String.
1. Asc function
This string function in VB.Net is used in order to get the integer value of the first letter of the string. Its integer value is actually the integer value of that character.
Example
Input
Dim Str as String
Str="Latin"
Asc(Str)
Output: 76
2. Format function
This function is used to arrange the string in a particular format. Here we will consider an example that will change the data representation.
Example
Input
Dim ChangedTime As Date = #03/23/2019 4:10:43 PM#
Dim ChangedTime as the string
ChangedTime = Format(TestDateTime, "h:m:s")
Output: 04:10:43 PM
3. Join function
This VB.Net String function is used to join two substrings. Here in this example, we will create an array of string and then add the value in the array with a comma(,).
Example
Input
Dim ItemList() As String = {"Apple", "Banana", "Guava"}
Dim JoinItemList as string = Join(ItemList, ", ")
Output: Apple, Banana, Guava
4. LCase Function
This function will convert all the characters of the string into a lowercase character. If the character is already in lowercase that it will ignore the character else will convert that into lowercase.
Example
Input
Dim Uppercase as String = "HELLO WORLD"
Dim Lowercase as String = LCase(Uppercase)
Output: hello world
5. Left function
This function will return the specific characters from left as requested by mentioning any number. If we will use this function to get the first four characters from the string from the left end than we have to mention the number 4.
Example
Input
Dim CheckStr as string = "Hey Jim"
Dim ResultStr as string = Left(CheckStr, 3)
Output: Hey
6. Len function
This String function in VB.Net will return the numbers of characters in a string. The value returned will be an integer value so it has to be stored in the integer variable.
Example
Input
Dim StrWords as String = "You are a hero!"
Dim WordCount as Integer = Len(StrWords)
Output: 15
7. Right Function
This function will return the specified number of characters from a string from the right side. The way the Left function has worked, it will work similarly. The only difference will be that it will select the characters from the right.
Example
Input
Dim CheckStr as string = "Hey Jim"
Dim ResultStr as string = Right(CheckStr, 3)
Output: Jim
8. Split function
This String function in VB.Net is used to split the string. Though there is various delimiter that could be used with function and here we will just separate it with space.
Example
Input
Dim CheckStr as String = "How are you?"
Dim OutputStr as String = Split(CheckStr)
Output: {“How”, “are”, “you?”}, it is the array of string actually.
9. StrReverse function
This function will be used to reverse the value of the string. It sounds the same as swapping the characters to revert the string. Let’s see an example
Example
Input
Dim CorrectStr as String = "Apple"
Dim ReverseString as String = StrReverse(CorrectStr)
Output: elppA
10. UCase function
This VB.Net String function will turn all the lowercase characters of the string into uppercase. It works exactly the reverse as the Lcase function does.
Example
Input
Dim LowercaseStr as String = "Hello Jim"
Dim UppercaseStr as String = UCase(LowercaseStr)
Output
HELLO JIM
Conclusion
The string is a very useful data type in any programming language. Almost all of us are unaware that it plays a very vital role in securing the user ID and password. There are several complex algorithms called hashing algorithms that only manipulates the string to add an edge in the security of data. To work with String all its need is to understand the possibility that string functions possess.
Recommended Articles
This has been a guide to VB.Net String Functions. Here we have discussed basic concept, working of String functions in Vb.Net with appropriate examples. You may also look at the following articles to learn more –