May 13, 2021 vb.net
In VB.Net, strings can be used as an array of characters, but it is more common to declare a string variable using the String keyword. T he string keyword is an alias for the System.String class.
You can create string objects using one of the following methods:
By assigning a string literal to a String variable
by specifying a string to a string variable
By using a String class constructor by
using the String class constructor
By using the string concatenation operator (-) by
using the string connection operator
By retrieving a property or calling a method that returns a string by retrieving properties or calling methods that return strings
By calling a formatting method to convert a value or object to its string representation
Convert a value or object to its string representation by calling a formatting method
The following example illustrates this:
Module strings Sub Main() Dim fname, lname, fullname, greetings As String fname = "Rowan" lname = "Atkinson" 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", "Tutorials", "Point"} Dim message As String = String.Join(" ", sarray) Console.WriteLine("Message: {0}", message) 'formatting method to convert a value Dim waiting As DateTime = New DateTime(2012, 12, 12, 17, 58, 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 results:
Full Name: Rowan Atkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 5:58 PM on Wednesday, December 12, 2012
The String class has two properties:
SN | The name and description of the property |
---|---|
1 |
Chars
Gets the Char object at the specified location in the current String object. |
2 |
Length
Gets the number of characters in the current String object. |
The String class has many ways to help you work with string objects. T he following table provides some of the most commonly used methods:
SN | Method name and description |
---|---|
1 |
Public Shared Function Compare ( strA As String, strB As String ) As Integer
Compare the two specified string objects and return an integer indicating their relative position in the sort order. |
2 |
Public Shared Function Compare ( strA As String, strB As String, ignoreCase As Boolean ) As Integer
Compare the two specified string objects and return an integer indicating their relative position in the sort order. H owever, if the Boolean parameter is true, it ignores case. |
3 |
Public Shared Function Concat ( str0 As String, str1 As String ) As String
Connect two string objects. |
4 |
Public Shared Function Concat ( str0 As String, str1 As String, str2 As String ) As String
Connect three string objects. |
5 |
Public Shared Function Concat (str0 As String, str1 As String, str2 As String, str3 As String) As String public sharing function Concat (str0 As String, str1 As String, str2 As String, str3 As String) As String Connect four string objects. |
6 |
Public Function Contains ( value As String ) As Boolean
Returns a value indicating whether the specified string object appears in the string. |
7 |
Public Shared Function Copy ( str As String ) As String
Create 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 )
Copy a specified number of characters from the specified location of the string object to the specified location in the Unicode character array. |
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 the current string object and specifies whether the string object has the same value. |
11 |
Public Shared Function Equals ( a As String, b As String ) As Boolean
Determines whether the two specified string objects have the same value. |
12 |
Public Shared Function Format ( format As String, arg0 As Object ) As String
Replace one or more formatting items in the specified string with a string representation of the specified object. |
13 |
Public Function IndexOf ( value As Char ) As Integer
Returns the zero-based index in the current string that specifies that the Unicode character first appears. |
14 |
Public Function IndexOf ( value As String ) As Integer
Returns the zero-start index in this instance that specifies that the string first appears. |
15 |
Public Function IndexOf ( value As Char, startIndex As Integer ) As Integer
Returns the zero-based index in this string that specifies the first appearance of the Unicode character, starting the search from the specified character position. |
16 |
Public Function IndexOf ( value As String, startIndex As Integer ) As Integer
Returns the zero-based index in this instance where the specified string first appears, and begins the search at the specified character location. |
17 |
Public Function IndexOfAny ( anyOf As Char() ) As Integer
Returns a zero-based index that first appears in this instance of any character in the specified Unicode character array. |
18 |
Public Function IndexOfAny ( anyOf As Char(), startIndex As Integer ) As Integer
Returns the zero-based index that first appears in this instance of any character in the specified Unicode character array, starting the search at the specified character location. |
19 |
Public Function Insert ( startIndex As Integer, value As String ) As String
Returns a new string, where the specified string is inserted into the index position specified in the current string object. |
20 |
Public Shared Function IsNullOrEmpty ( value As String ) As Boolean
Indicates whether the specified string is empty or empty. |
21 |
Public Shared Function Join ( separator As String, ParamArray value As String() ) As String
Connect all elements of the string array, using the separator specified between each element. |
22 |
Public Shared Function Join ( separator As String, value As String(), startIndex As Integer, count As Integer ) As String
Connect the specified elements of the string array with a separator specified between each element. |
23 |
Public Function LastIndexOf ( value As Char ) As Integer
Returns the zero-based index position in the current string object that specifies the last time the Unicode character appeared. |
24 |
Public Function LastIndexOf ( value As String ) As Integer
Returns the zero-based index position in the current string object that specifies the last time the string appeared. |
25 |
Public Function Remove ( startIndex As Integer )
As String
Remove all characters from the current instance, start at the specified location, continue to the last location, and return the string. |
26 |
Public Function Remove ( startIndex As Integer, count As Integer ) As String
Remove the specified number of characters from the current string from the specified location and return the string.
|
27 |
Public Function Replace ( oldChar As Char, newChar As Char ) As String
Replace all appearances of the specified Unicode character in the current string object with the specified Unicode character and return a new string. |
28 |
Public Function Replace ( oldValue As String, newValue As String ) As String
Replace all appearances of the specified string in the current string object with the specified string and return a new string. |
29 |
Public Function Split ( ParamArray separator As Char() ) As String()
Returns an array of strings that contain substrings in the current string object, separated by elements of the specified array of Unicode characters. |
30 |
Public Function Split ( separator As Char(), count As Integer ) As String()
Returns an array of strings that contain substrings in the current string object, separated by elements of the specified array of Unicode characters. T he 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 an array of Unicode characters that contain all the characters in the current string object. |
33 |
Public Function ToCharArray ( startIndex As Integer, length As Integer ) As Char()
Returns an array of Unicode characters that contain all the characters in the current string object, starting with the specified index until the specified length. |
34 |
Public Function ToLower As String
Returns a converted, small copy of this string. |
35 |
Public Function ToUpper As String
Returns a converted capital copy of this string. |
36 |
Public Function Trim As String
Remove all leading and tail space characters from the current String object. |
The methods listed above are not exhaustive, please visit the MSDN library for a complete list of methods and String class constructors.
The following example illustrates some of the above methods:
Comparison string:
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 results:
This is test and This is text are not equal.
A string contains a string:
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 results:
The sequence 'test' was found.
Substrings obtained:
Module strings Sub Main() Dim str As String str = "Last night I dreamt of San Pedro" 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 results:
Last night I dreamt of San Pedro San Pedro.
Add a string:
Module strings Sub Main() Dim strarray As String() = {"Down the way where the nights are gay", "And the sun shines daily on the mountain top", "I took a trip on a sailing ship", "And when I reached Jamaica", "I made a stop"} 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 results:
Down the way where the nights are gay And the sun shines daily on the mountain top I took a trip on a sailing ship And when I reached Jamaica I made a stop