Home » VB.NET String

VB.NET String

In VB.NET, string is a sequential collection of characters that is called a text. The String keyword is used to create a string variable that stores the text value. The name of the string class is System.String that contains the various function of string.

Declaration and Initialization of String

The following are the different ways to declare and initialize the string variable using the String keyword in the VB.NET programming language.

In the above declaration of string, we have defined the string variable with string keyword and initialize string variable with value based on our requirements.

Creating a String Object

There are different ways to create a string object in the String.

  1. To use a String class constructor.
  2. By using the property or calling method to return a string.
  3. Use a string literal to define a string variable.
  4. Use a String concatenation operator (+).
  5. By using a formatting function to convert a value or object into the string representation of the object.

Let’s create an example to demonstrates the uses of String objects.

String_object.vb

Output:

VB.NET String

Immutable string object

In VB.NET, the string object is immutable. It means that once we have created a string object, it cannot be modified during its execution. And if we modified an existing value in a string object through addition and subtraction, it discards the old value of instance in memory and creates a new instance to hold a new value. Furthermore, if we want to perform any operation in the String object, we must define an object every time to create a new String object. So, in that case, the string provides the System.Text.StringBuilder class to modify a string without creating a new object.

Properties of the String Class

Following are the two properties of string class:

  1. Chars: It is used to get the character from the current string object.
  2. Length: It is used to return the number of characters in the current string object.

Access individual characters from Strings in VB.NET

In VB.NET, we can access the individual character from the current string object with index values.

Let’s write a program to access the individual characters from the string by defining an index position.

Get_Chars.vb

Output:

VB.NET String

In the above program, a For loop is continuously executed each element of the String variable’s value is Hello World! Until the condition (name.Length -1) is reached. Here, Length function is used to count the number of characters in the string.

There are various functions of string class that helps to manipulate the string objects.

1. Compare(): It is used to compare two specified string objects, and if the string is less than another string object, it returns an integer value.

Syntax:

The returned integer value represents the relationship between two specified string objects. The following table shows the string comparison condition that returns the integer value after comparing two specified string items.

Compare String Condition It returns an integer value
Str1 == Str2 0
Str1 > Str2 1
Str1 < Str2 -1

Example: Write a program to understand the uses of Compare() function in String Class.

String_cmp.vb

Output:

VB.NET String

In the above example, the string compare () method checks the values of two string variables and returns integer values.

2. Concat()

The Concat() function is used in string class to join two or more strings or append one string to the end of another string. It is useful when we need to combine two or more text to make a sentence, and it returns a new string.

Syntax:

And when we use more than three string parameters to combine, we have to define this way:

Syntax:

Let’s create a program to understand the uses of the Concat() function to append or concatenate one or more strings in the VB.NET programming language.

String_concate.vb

Output:

VB.NET String

In the above program, we used a Concat() function to combine one or more strings to return a single string.

3. Contains(): It is used to validate whether the specified text or parameter exists in the current string. If the text is existing in the string, it returns a true Boolean value; otherwise, it returns false.

Syntax:

Let’s create a program to understand the uses of the Contains() function to check whether the particular string exists or not in a given string.

String_contain.vb

Output:

VB.NET String

In the above program, the String Contains() function is to determine whether the given string str contains the value of strtext, and if the value exists in the str, it returns a true value; otherwise, it returns false.

4. String Copy(): In the String class, there is a Copy() function used to create a new string object with the same content or value specified in the original string.

Syntax:

Let’s create a program to understand the uses of Copy() function in the VB.NET programming language.

String_copy.vb

Output:

VB.NET String

5. CopyTo(): A CopyTo () function is used to copy the specified number of characters from the original string object.

Syntax:

Create a simple program of the CopyTo() function to copy a string in the VB.NET programming language.

String_CopyTo.vb

Output:

VB.NET String

6. EndsWith(): The EndsWith() function is used to check whether the text or parameter specified in the original string ended with the specified text.

Syntax:

Let’s create a program to understand the uses of EndsWith() function in the VB.NET programming language.

String_With.vb

Output:

VB.NET String

In the above program, the EndsWith() function is checked whether the defined text ‘language’ specified in the original string object str ends with the word language.

7. Equals(): As the name defines, an Equals() function is used in the string class to determine if both string objects have the same value or not. If the value is the same, the Equals() string methods are true; otherwise, it returns false. And if both objects have null values, the Equals() function returns a true value.

Syntax:

Let’s create a program to understand the uses of EndsWith() function in the VB.NET programming language.

Equal_String.vb

Output:

VB.NET String

In the above example, the String Equals() method checks whether the specified strings object is equal. If yes, it returns True value; otherwise, it returns False.

8. String Format(): The Format() function is used to replace one or more objects with the specified string object. By using the Format() function, we can insert the value of a variable into another object string.

Syntax:

Let’s create a program to understand the uses of Format() function in the VB.NET programming language.

String_Format.vb

Output:

VB.NET String

In the above example, the string Format() method has replaced the format objects {0} with “VB.NET”, {1} with “String” and {2} with “Format_String”.

9. IndexOf(): The String IndexOf() function is used to return the index position of the first occurrence of the specified character or string in the original string object, followed by the zero (0) index. Furthermore, we can also define the starting index position of the specified character or string in the original string.

Syntax:

Let’s create a program to understand the uses of IndexOf() function in VB.NET programming language.

indexOf_String.vb

Output:

VB.NET String

In the above example, we used an IndexOf() function to get the specified position of a character or string from the original string in VB.NET programming.

10. Insert(): The Insert() function of the String class is used to insert a specified string at the specified position in the original string object.

Syntax:

Let’s create a program to understand the uses of Insert() function in the VB.NET programming language.

String_Insert.vb

Output:

VB.NET String

11. Remove(): The remove function is used to remove or delete characters at the specified position until the end of the defined string object. Furthermore, we can use the remove () function to remove a set of characters by defining the beginning and end positions of the characters to be removed from the original string.

Syntax:

Let’s create a program to understand the uses of Insert() function in the VB.NET programming language.

String_Remove.vb

Output:

VB.NET String

12. Replace(): The String Replace() function is used to replace all the occurrence of a specified character or string from the original string object and then return a new string.

Syntax:

Let’s create a program to understand the use of Replace() function in the VB.NET programming language.

String_Replace.vb

Output:

VB.NET String

In the above example, we used a Replace() function to replace a specified character or string from the given string in the VB.NET programming.

13. Join(): As the name suggests, a Join() function is used to concatenate all the specified elements of the string array using the specified separator.

Syntax

Let’s create a program to understand the uses of Join() function in the VB.NET programming language.

String_Join

Output:

VB.NET String

In the above program of String Join() function, we used hyphen (-) and comma (,) separator to concatenates all the elements of the string array.

14. Split(): As the name suggests, the string Split() function is used to split a given string into substrings with the help of delimiters that are used to split a string in VB.NeT programming language. Furthermore, we can specify the number of characters that should be returned in the substring.

Syntax:

Let’s create a program to understand the uses of the Split() function in the VB.NET programming language.

String_Split.vb

Output:

VB.NET String

In the above example, we used a Split() function to split the given string (Welcome-to-tutoraspire”) with a separator hyphen (-), and it returns a split string as an array. And the For loop is used to display each item of an array element.

15. StartsWith(): The StartsWith() function of String class is used to determine whether the string’s beginning is matched with a specified string object. If the string is matched, it returns True value; otherwise, it shows False.

Syntax:

Let’s create a program to understand the uses of StartsWith() function in the VB.NET programming language.

String_StartsWith.vb

In the above program, we used StartsWith() function to validate whether the given string starts with “Welcome”. If yes, returns True; otherwise, it shows False.

16. ToLower: The ToLower function of String class is used to convert any string object into Lower Case.

17. ToUpper: The ToUpper function of String class is used to convert a string object into Upper Case.

Let’s create a program to understand the uses of ToLower and ToUpper function in the VB.NET programming language.

String_cases.vb

Output:

VB.NET String

In the above program, we used ToLower and ToUpper method in String class to convert a given string into the Upper and Lower case of the VB.NET programming.

18. Trim(): The Trim() Function is used to remove all occurrence of leading and trailing whitespace, characters and special symbols from the specified string object.

Syntax:

The First Syntax of Trim() function represents that it is used to remove all whitespaces from the starting and end of the given string.

The Second Syntax represents that it is used to trim all specified characters from the starting and end of the given string object.

Let’s create a program to understand the uses of the Trim() function in the VB.NET programming language.

Trim.vb

Output:

VB.NET String


Next TopicVB.NET ArrayList

You may also like