What is String in VB.net?
The String in VB.net is a sequence of characters collectively referred to as a string.
The text value is saved in a string variable that is created using the String keyword. System.String is the name of the string class, which includes all of the string’s functions.
In VB.net, you can use strings as an array of characters, however, the more common practice is to use the String keyword to declare a string variable.
Declaration and Initialization of String in VB.net
The following are the different ways to Declare and Initialize the String Variable using the String Keyword in VB.net.
'Declaration of the String variable
Dim str As String
Dim abc As String
' Initialization of String variable
Dim str As String = "Welcome to IT SOURCECODE."
Dim str1 As String = "Hello World!"
' Initialize a null string
Dim str2 As String = Nothing
' Initialization of an empty string
Dim name As String = String.Empty
' Creating a String from char
Dim letter As Char() = {"H", "E", "L", "L", "O" }
In the above declaration of string, we have defined the string variable with the string keyword and initialized the string variable with value based on our requirements.
Creating A String Object in VB.net
You can create a String Object in VB.net using one of the following methods:
- By assigning a string literal to a String variable
- By using a String class constructor
- By using the string concatenation operator (+)
- By retrieving a property or calling a method that returns a string
- By calling a formatting method to convert a value or object to its string representation
Let’s create an example to demonstrate the uses of String objects in VB.net.
Module strings
Sub Main()
Dim fname, lname, fullname, greetings As String
fname = "Angel Jude"
lname = "Suarez"
fullname = fname + " " + lname
Console.WriteLine("Full Name: {0}", fullname)
'by using string constructor
Dim letters As Char() = {"H", "e", "l", "l", "o"}
greetings = New String(letters)
Console.WriteLine("Greetings: {0}", greetings)
'methods returning String
Dim sarray() As String = {"Hello", "From", "IT", "SOURCECODE"}
Dim message As String = String.Join(" ", sarray)
Console.WriteLine("Message: {0}", message)
'formatting method to convert a value
Dim waiting As DateTime = New DateTime(2022, 06, 25, 12, 00, 1)
Dim chat As String = String.Format("Message sent at {0:t} on {0:D}", waiting)
Console.WriteLine("Message: {0}", chat)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
Full Name: Angel Jude Suarez
Greetings: Hello
Message: Hello From IT SOURCECODE
Message: Message sent at 12:00 PM on Saturday, June 25, 2022
You can test the above example here! ➡ VB.net Online Compiler
Immutable String Object in VB.net
The String Object In VB.net is immutable. It means that once we have created a string object, it cannot be modified during its execution.
If we modify an existing value in a string object through addition and subtraction, it discards the old value of an 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 in VB.net
The String Class in VB.net has the following two properties:
1. Chars
Gets the Char object at a specified position in the current String object.
2. Length
Gets the number of characters in the current String object
Methods of the String Class in VB.net
The String class has numerous methods that help you in working with the String Objects in VB.net.
The following table provides some of the most commonly used methods:
# | Methods of the String Class in VB.net : Method Name & Description |
---|---|
1. | Public Shared Function Compare ( strA As String, strB As String ) As Integer Compares two specified string objects and returns an integer that indicates their relative position in the sort order. |
2. | Public Shared Function Compare ( strA As String, strB As String, ignoreCase As Boolean ) As Integer Compares two specified string objects and returns an integer that indicates their relative position in the sort order. However, it ignores case if the Boolean parameter is true. |
3. | Public Shared Function Concat ( str0 As String, str1 As String ) As String Concatenates two string objects. |
4. | Public Shared Function Concat ( str0 As String, str1 As String, str2 As String ) As String Concatenates three string objects. |
5. | Public Shared Function Concat (str0 As String, str1 As String, str2 As String, str3 As String ) As String Concatenates four string objects. |
6. | Public Function Contains ( value As String ) As Boolean Returns a value indicating whether the specified string object occurs within this string. |
7. | Public Shared Function Copy ( str As String ) As String Creates a new String object with the same value as the specified string. |
8. | pPublic Sub CopyTo ( sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer ) Copies a specified number of characters from a specified position of the string object to a specified position in an array of Unicode characters. |
9. | Public Function EndsWith ( value As String ) As Boolean Determines whether the end of the string object matches the specified string. |
10. | Public Function Equals ( value As String ) As Boolean Determines whether the current string object and the specified string object have the same value. |
11. | Public Shared Function Equals ( a As String, b As String ) As Boolean Determines whether two specified string objects have the same value. |
12. | Public Shared Function Format ( format As String, arg0 As Object ) As String Replaces one or more format items in a specified string with the string representation of a specified object. |
13. | Public Function IndexOf ( value As Char ) As Integer Returns the zero-based index of the first occurrence of the specified Unicode character in the current string. |
14. | Public Function IndexOf ( value As String ) As Integer Returns the zero-based index of the first occurrence of the specified string in this instance. |
15. | Public Function IndexOf ( value As Char, startIndex As Integer ) As Integer Returns the zero-based index of the first occurrence of the specified Unicode character in this string, starting search at the specified character position. |
16. | Public Function IndexOf ( value As String, startIndex As Integer ) As Integer Returns the zero-based index of the first occurrence of the specified string in this instance, starting search at the specified character position. |
17. | Public Function IndexOfAny ( anyOf As Char() ) As Integer Returns the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. |
18. | Public Function IndexOfAny ( anyOf As Char(), startIndex As Integer ) As Integer Returns the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters, starting search at the specified character position. |
19. | Public Function Insert ( startIndex As Integer, value As String ) As String Returns a new string in which a specified string is inserted at a specified index position in the current string object. |
20. | Public Shared Function IsNullOrEmpty ( value As String ) As Boolean Indicates whether the specified string is null or an Empty string. |
21. | Public Shared Function Join ( separator As String, ParamArray value As String() ) As String Concatenates all the elements of a string array, using the specified separator between each element. |
22. | Public Shared Function Join ( separator As String, value As String(), startIndex As Integer, count As Integer ) As String Concatenates the specified elements of a string array, using the specified separator between each element. |
23. | Public Function LastIndexOf ( value As Char ) As Integer Returns the zero-based index position of the last occurrence of the specified Unicode character within the current string object. |
24. | Public Function LastIndexOf ( value As String ) As Integer Returns the zero-based index position of the last occurrence of a specified string within the current string object. |
25. | Public Function Remove ( startIndex As Integer ) As String Removes all the characters in the current instance, beginning at a specified position and continuing through the last position, and returns the string. |
26. | Public Function Remove ( startIndex As Integer, count As Integer ) As String Removes the specified number of characters in the current string beginning at a specified position and returns the string. |
27. | Public Function Replace ( oldChar As Char, newChar As Char ) As String Replaces all occurrences of a specified Unicode character in the current string object with the specified Unicode character and returns the new string. |
28. | Public Function Replace ( oldValue As String, newValue As String ) As String Replaces all occurrences of a specified string in the current string object with the specified string and returns the new string. |
29. | Public Function Split ( ParamArray separator As Char() ) As String() Returns a string array that contains the substrings in the current string object, delimited by elements of a specified Unicode character array. |
30. | Public Function Split ( separator As Char(), count As Integer ) As String() Returns a string array that contains the substrings in the current string object, delimited by elements of a specified Unicode character array. The int parameter specifies the maximum number of substrings to return. |
31. | Public Function StartsWith ( value As String ) As Boolean Determines whether the beginning of this string instance matches the specified string. |
32. | Public Function ToCharArray As Char() Returns a Unicode character array with all the characters in the current string object. |
33. | Public Function ToCharArray ( startIndex As Integer, length As Integer ) As Char() Returns a Unicode character array with all the characters in the current string object, starting from the specified index and up to the specified length. |
34. | Public Function ToLower As String Returns a copy of this string converted to lowercase. |
35. | Public Function ToUpper As String Returns a copy of this string converted to uppercase. |
36. | Public Function Trim As String Removes all leading and trailing white-space characters from the current String object. |
Example Programs of the Methods of the String Class in VB.net
The following example demonstrates some of the methods mentioned above.
Example Program of Comparing String in VB.net:
Module strings
Sub Main()
Dim str1, str2 As String
str1 = "This is test"
str2 = "This is text"
If (String.Compare(str1, str2) = 0) Then
Console.WriteLine(str1 + " and " + str2 + " are equal.")
Else
Console.WriteLine(str1 + " and " + str2 + " are not equal.")
End If
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
This is test and This is text are not equal.
You can test the above example here! ➡ VB.net Online Compiler
Example Program of String Contain String in VB.net:
Module strings
Sub Main()
Dim str1 As String
str1 = "This is test"
If (str1.Contains("test")) Then
Console.WriteLine("The sequence 'test' was found.")
End If
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
The sequence ‘test’ was found.
You can test the above example here! ➡ VB.net Online Compiler
Example Program of Getting a Substring in VB.net:
Module strings
Sub Main()
Dim str As String
str = "I am a Programmer and I have no Life"
Console.WriteLine(str)
Dim substr As String = str.Substring(23)
Console.WriteLine(substr)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
I am a Programmer and I have no Life
have no Life
You can test the above example here! ➡ VB.net Online Compiler
Example Program of Joining Strings in VB.net:
Module strings
Sub Main()
Dim strarray As String() = {
"I am Angel Jude Suarez",
"And i am graduated of Bachelor of Science in Information Technology",
"I am a programmer",
"And I have no life"
}
Dim str As String = String.Join(vbCrLf, strarray)
Console.WriteLine(str)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
I am Angel Jude Suarez
And i am graduated of Bachelor of Science in Information Technology
I am a programmer
And I have no life
You can test the above example here! ➡ VB.net Online Compiler
Summary
In this tutorial we discuss the String Function in VB.net with Example, Declaration, and Initialization of String in VB.net, and the Method of String Class in VB.net.
In visual basic, String is a keyword and it is useful to represent a sequential collection of characters that is called a text.
In visual basic, the String keyword is useful to create a string variable to hold the text which is a sequential collection of characters.
PREVIOUS
NEXT