VB.NET String

VB.NET String: String is a sequence of characters called as text. A string variable that stores the text value is created by the String Keyword. The string class contains several functions of string named as System.String.

The String object cannot be modified once created as the String object is Immutable. This means that when using an operation in String object, a new String Object is created. Thus, in conditions like continued operations with String Object, it is suggested to use System.Text.StringBuilder.

System.Text.StringBuilder class helps in modifying a string instead of creation of the new object.

Declaring and Initializing in VB.NET

Firstly the string is declared in the program. Then only, it can be used multiple number of times. Given below is the syntax for defining a string in VB .net.

Dim Str as String

  • Dim: This keyword is used for defining any variable.
  • Str: This variable holds the string value.
  • String: This keyword creates variable to store text value.

After declaring the variable, some values are inserted in it, and those values can be used in the program. The assigning of value for variables can be done manually or it can be inserted be the user.

Let’s see how the values could be assigned manually.

Str=“Latin”                                                                                                                                     

In this, the variable str is assigned with a value. The values must be written in double quotes always. After assigning the values, it can be used in the whole program.

Creating a String Object

The String Object can be created through several ways.

  1. By using a String class constructor.
  1. Through, the use of property or by the calling method for returning a string.
  2. Using a string literal for defining a string variable.
  3. Use a String concatenation operator (+).
  4. By using a formatting function to convert a value or object into the string representation of the object.

VB.Net String Functions

This section shows how to use the string with different functions for processing values and producing the desired output. Below are the string functions that are used to work with String.

1.Asc functionThis function helps in getting the integer value for the first letter of string. The value of integer in this is the value of that character.   Example   Input                                                     Output: 76 Dim Str as String
Str=“Latin”
Asc(Str)  
2.Format FunctionThis function helps in arranging string in proper format. Given below is an example of changing the representation of day.   Example   Input
Dim ChangedTime As Date = #11/11/2020 11:15:50 PM#
Dim ChangedTime as the string
ChangedTime = Format(TestDateTime, "h:m:s")   Output: 11:15:50 PM  
3.Join FunctionIn this function, two substrings are joined together. Given below is the example of creating a string array and adding values in array with comma (,).   Example   Input Dim ItemList() As String = {“Peanut”, “Almond”, “Cashew”}
Dim JoinItemList as string = Join(ItemList, ", ")   Output: Peanut, Almond, Cashew
4.LCase FunctionWith the help of this function, all characters in string will be converted into lowercase. In case, 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 = “WELCOME TO TUTORIAL WITH EXAMPLE”
Dim Lowercase as String = LCase(Uppercase)   Output: welcome to tutorial with example  
5.Left FunctionIn this function, the characters returns from the left as the number mentions. Suppose we want first five characters from left end of string, then we will mention number 5.   Example   Input Dim CheckStr as string = “Hello Rishi”
Dim ResultStr as string = Left(CheckStr, 5)   Output: Hello  
6.Len FunctionIn this function, the value of total number of characters in string is returned. It will be stored by integer variable as the return value will be integer.         Example   Input Dim StrWords as String = “This tutorial is regarding VB.NET”
Dim WordCount as Integer = Len(StrWords)   Output: 33
      7.      Right Function      In this function, the characters return from the right side of the string as the number mentions. Suppose we want first five characters from right end of string, then we will mention number 5.           Example   Input Dim CheckStr as string = “Hello Rishi”
Dim ResultStr as string = Right(CheckStr, 3)   Output: Rishi  
8.Split FunctionIn this function, the splitting of a string is performed. The separation of strings is done with the help of space. No other forms of delimiters are required for it.   Example   Input Dim CheckStr as String = “Tutorial with example”
Dim OutputStr as String = Split(CheckStr)   Output: {“Tutorial”, “with”, “example”}, it is the array of string actually.  
9.StrReverse FunctionWith the help of this function, the value of the string is reversed. Here is an example of this function.   Example   Input Dim CorrectStr as String = “website”
Dim ReverseString as String = StrReverse(CorrectStr)   Output: etisbew  
10.UCase FunctionWith the help of this function, all the lowercase characters are turned into the uppercase characters. In case, a character is in uppercase previously, it will be remain unchanged.   Example   Input: Dim LowercaseStr as String =“welcome to tutorial with example” Dim UppercaseStr as String = UCase(LowercaseStr)   Output:  WELCOME TO TUTORIAL WITH EXAMPLE